PHP 通过引用传递数组元素
我有两个数组,一个是索引数组,一个是关联数组。我的问题归结为,如何将关联数组的引用传递给编辑类。这样,当有更多书籍和电影时,我可以循环浏览,清理所有 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);
}
}
I have two arrays, one indexed and one associative. What my question boils down to is, how can I pass the associative array's reference to the edit class. This way when there are more books and movies, I can loop through, clean all the isbn's and not touch movie. The problem I'm having is passing the reference inside the for loop.
$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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其发布在这里作为参考,因为这个问题已在注释中解决
,执行
&$assoc[key($assoc)]
将解决问题。Posting it here as reference since this was solved in the comments
doing
&$assoc[key($assoc)]
will solve the problem.