PHP 通过引用传递问题 - 无法更改类型?
我有一个奇怪的现象。我希望有人能向我解释那里发生了什么: 我想创建一个过滤器。原点类似于“-10”或“10-20”或“20+”(字符串类型),结果应为“低于 10 美元”,...以及“product_price <<”。 10', ... 对于 sql 命令。
但是将数组存储回原始字符串是行不通的。它只是提供“$Array”作为结果。难道不能通过引用传递并改变类型吗?
感谢您的知识!
foreach($filters as &$filter){
preg_match ('#^\-(\d+)$#ism', $filter, $match);
if ($match[1]){
$filter = array(
'Under $'.intval($match[1]),
'product_price < '.intval($match[1])
);
}
...
}
return $filtering;
}
PS:我不是在寻找解决方案,因为我可以将原始字符串更改为数组,或者我可以将 foreach
更改为 按值传递
并创建一个新的带有 $newFilter[] = ... 之类的数组的数组我只是好奇
I have a weird phenomenon. I hope someone can explain to me what is happening there:
I want to create a filter. The origin is something like '-10' or '10-20' or '20+' (type string) and the result should be 'Under $10', ... as well as 'product_price < 10', ... for a sql command.
But storing the array back on the original string doesn't work. It just delivers '$Array' as result. Is it not possible to pass by reference and change the type?
Thanks for your knowledge!
foreach($filters as &$filter){
preg_match ('#^\-(\d+)$#ism', $filter, $match);
if ($match[1]){
$filter = array(
'Under
P.S.: I am not looking for a solution, because I could change the origin string into array, or I could change the foreach
in to a pass by value
and create a new array with the arrays like $newFilter[] = ... I am only curious
.intval($match[1]),
'product_price < '.intval($match[1])
);
}
...
}
return $filtering;
}
P.S.: I am not looking for a solution, because I could change the origin string into array, or I could change the foreach
in to a pass by value
and create a new array with the arrays like $newFilter[] = ... I am only curious
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以更改它的类型。构造证明:
打印:
You can change it's type. Proof by construction:
Prints:
您应该将 foreach 更改为
并更新您的过滤器,
我相信 foreach() 语句创建的 $filter 变量是数组中数据的副本,而不是对其的引用。
You should change your foreach into
and update your filter by doing
I believe the $filter variable created by the foreach() statement is a copy of the data in the array and not a reference to it.