Kev*_*vin 3 php regex pass-by-reference
我有两个数组,一个是索引的,一个是关联的.我的问题归结为,如何将关联数组的引用传递给编辑类.这样,当有更多的书籍和电影时,我可以循环,清理所有的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);
}
}
Run Code Online (Sandbox Code Playgroud)
在此处将其作为参考发布,因为这已在评论中得到解决
做&$assoc[key($assoc)]会解决问题.
for($x = 0; $x < count($index); $x++){
if($index[$x]["book"] == key($assoc)){
edit::modify(&$assoc[key($assoc)]);
}
next($assoc);
}
Run Code Online (Sandbox Code Playgroud)