数组值意外变化
我正在使用 cakephp 1.2,并且我有一个数组,即使该变量没有被操作,它的值似乎也发生了变化。下面是给我带来麻烦的代码。
请注意 - 更新更改变量名称对结果没有影响。
function findCountByString($string, $myArr=array()) {
$main_conditions['or'] = array();
$main_conditions['or']['Article.title LIKE '] = '%'.$string.'%';
$main_conditions['or']['Article.html_content LIKE '] = '%'.$string.'%';
$conditions['and'][] = $main_conditions;
$filter_conditions['or'] = array();
if(count($myArr) > 0) {
# UPDATE NUMBER 2
# if I comment out the below line everything is fine, this makes no sense!!!
$filter_conditions['or']['ArticleEntity.entity_id'] = $myArr;
$conditions['and'][] = $filter_conditions;
}
echo "Start of findCountByString()";
var_dump($myArr);
$test = $this->find('count', array(
'conditions' => $conditions,
'joins' => array('LEFT JOIN `articles_entities` AS ArticleEntity ON `ArticleEntity`.`article_id` = `Article`.`id`'),
'group' => 'Article.id'
));
echo "End of findCountByString()";
var_dump($myArr);
return $test;
我
得到以下输出:
Start of findCountByString()
array(4) {
[0]=>
string(36) "4bdb1d96-c680-4c2c-aae7-104c39d70629"
[1]=>
string(36) "4bdb1d6a-9e38-479d-9ad4-105c39d70629"
[2]=>
string(36) "4bdb1b55-35f0-4d22-ab38-104e39d70629"
[3]=>
&string(36) "4bdb25f4-34d4-46ea-bcb6-104f39d70629"
}
End of findCountByString()
array(4) {
[0]=>
string(36) "4bdb1d96-c680-4c2c-aae7-104c39d70629"
[1]=>
string(36) "4bdb1d6a-9e38-479d-9ad4-105c39d70629"
[2]=>
string(36) "4bdb1b55-35f0-4d22-ab38-104e39d70629"
[3]=>
&string(38) "'4bdb25f4-34d4-46ea-bcb6-104f39d70629'"
}
我的数组中的值已更改,但我不知道为什么?
有什么建议吗?
I am using cakephp 1.2 and I have an array that appears to have a value change even though that variable is not being manipulated. Below is the code to that is causing me trouble.
PLEASE NOTE - UPDATE Changing the variable name makes no difference to the outcome.
function findCountByString($string, $myArr=array()) {
$main_conditions['or'] = array();
$main_conditions['or']['Article.title LIKE '] = '%'.$string.'%';
$main_conditions['or']['Article.html_content LIKE '] = '%'.$string.'%';
$conditions['and'][] = $main_conditions;
$filter_conditions['or'] = array();
if(count($myArr) > 0) {
# UPDATE NUMBER 2
# if I comment out the below line everything is fine, this makes no sense!!!
$filter_conditions['or']['ArticleEntity.entity_id'] = $myArr;
$conditions['and'][] = $filter_conditions;
}
echo "Start of findCountByString()";
var_dump($myArr);
$test = $this->find('count', array(
'conditions' => $conditions,
'joins' => array('LEFT JOIN `articles_entities` AS ArticleEntity ON `ArticleEntity`.`article_id` = `Article`.`id`'),
'group' => 'Article.id'
));
echo "End of findCountByString()";
var_dump($myArr);
return $test;
}
I am getting the following output:
Start of findCountByString()
array(4) {
[0]=>
string(36) "4bdb1d96-c680-4c2c-aae7-104c39d70629"
[1]=>
string(36) "4bdb1d6a-9e38-479d-9ad4-105c39d70629"
[2]=>
string(36) "4bdb1b55-35f0-4d22-ab38-104e39d70629"
[3]=>
&string(36) "4bdb25f4-34d4-46ea-bcb6-104f39d70629"
}
End of findCountByString()
array(4) {
[0]=>
string(36) "4bdb1d96-c680-4c2c-aae7-104c39d70629"
[1]=>
string(36) "4bdb1d6a-9e38-479d-9ad4-105c39d70629"
[2]=>
string(36) "4bdb1b55-35f0-4d22-ab38-104e39d70629"
[3]=>
&string(38) "'4bdb25f4-34d4-46ea-bcb6-104f39d70629'"
}
The the value in my array have changed, and I don't know why?
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能
$filters
是一个引用,并且它在方法调用中被更改,或者pr
本身具有状态/副作用。可以通过删除对pr
的调用并将其替换为var_dump
来消除第二个选项。您的代码片段没有提供足够的信息。这里最好的选择是调试器。
编辑:
最后一个元素是一个引用(可能是 foreach 引用的剩余部分)。修复构建数组的代码,以便它不会在最后一个元素中留下引用。
Probably
$filters
is a reference and it's being changed in the method call orpr
itself has state/side effects. The second option can be eliminated by removing the calls topr
and replacing it withvar_dump
.Your code snippet does not provide sufficient information. Your best option here is a debugger.
EDIT:
Your last element is a reference (probably a remnant of a foreach by reference). Fix the code that builds the array so that it doesn't leave a reference in the last element.
似乎是访问错误通过引用的 PHP 数组。
由于 PHP 内部工作方式的特殊性,如果对数组的单个元素进行引用,然后复制该数组,无论是通过赋值还是在函数调用中按值传递,该引用都会被复制为大批。这意味着对任一数组中任何此类元素的更改都将在另一个数组(以及其他引用)中重复,即使数组具有不同的作用域(例如,一个是函数内的参数,另一个是全局的)!在复制时没有引用的元素以及在复制数组后分配给其他元素的引用将正常运行(即独立于其他数组)。
这个问题不会很快得到解决。这是实现中的一个根深蒂固的问题,解决它会导致速度问题和许多其他问题,这也是可以编码的东西,所以不应该造成巨大的问题。
http://bugs.php.net/bug.php?id=8130。
Seems like the bug with accessing a PHP array by reference.
Due to peculiarities of the internal workings of PHP, if a reference is made to a single element of an array and then the array is copied, whether by assignment or when passed by value in a function call, the reference is copied as part of the array. This means that changes to any such elements in either array will be duplicated in the other array (and in the other references), even if the arrays have different scopes (e.g. one is an argument inside a function and the other is global)! Elements that did not have references at the time of the copy, as well as references assigned to those other elements after the copy of the array, will behave normally (i.e. independent of the other array).
This will not be fixed anytime soon. It is a deep seated problem in the implementation and fixing it would cause speed problems and numerous other problems, This is somthing that can be coded around too so should not cause massive problems.
http://bugs.php.net/bug.php?id=8130.