【问题标题】:PHP pass array element by referencePHP通过引用传递数组元素
【发布时间】:2011-09-22 05:48:29
【问题描述】:

我有两个数组,一个是索引的,一个是关联的。我的问题归结为,如何将关联数组的引用传递给编辑类。这样当有更多的书和电影时,我可以循环浏览,清理所有的 isbn,而不是碰电影。我遇到的问题是在 for 循环中传递引用。

$i = new intro();

class intro{
  public function __construct(){
    $index = array(array("book", "regex"), array("movie", "regex"));
    $assoc = array(array("book"=>"freeBSD", "isbn"=>"01-2345-6789"), 
                   array("movie"=>"batman", "date"=>"10-10-1995");

    for($x = 0; $x < count($index); $x++){
      if($index[$x]["book"] == key($assoc)){
        edit::modify(current($assoc)); //I WANT TO PASS THE REFERENCE NOT VALUE
      }                                //current(&$assoc) DOES NOT WORK 
      next($assoc);
    }
  }
}

class edit{
  public function modify(&$isbn){
    $pattern = "/[^0-9]*/";
    $isbn = preg_replace($pattern, "", $isbn);
  }
}

【问题讨论】:

  • &amp;$assoc[key($assoc)] 怎么样?

标签: php regex pass-by-reference


【解决方案1】:

因为这在 cmets 中已解决,所以将其发布在这里作为参考

执行&amp;$assoc[key($assoc)] 将解决问题。

for($x = 0; $x < count($index); $x++){
  if($index[$x]["book"] == key($assoc)){
    edit::modify(&$assoc[key($assoc)]);
  }                                
  next($assoc);
}

【讨论】:

    猜你喜欢
    • 2016-04-09
    • 1970-01-01
    • 2015-07-04
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    相关资源
    最近更新 更多