Bug #70639 [Com]: Error use link in foreach
[email protected] ("inefedor at gmail dot com")
| Newsgroups | php.standards |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=70639&edit=1
ID: 70639
Comment by: inefedor at gmail dot com
Reported by: alex-dev at yandex dot ru
Summary: Error use link in foreach
Status: Open
Type: Bug
Package: PHP Language Specification
Operating System: Windows XP, Free BSD
PHP Version: 5.4.45
Block user comment: N
Private report: N
New Comment:
Hi
There's a problem in your comparison callback, you have the same condition in first and second 'if'.
And yes, after first foreach you have a $value is a reference to the last element of the array. So when you do the second foreach (without unsetting $value), it rewrites value of array's first element.
Previous Comments:
------------------------------------------------------------------------
[2015-10-05 09:41:13] alex-dev at yandex dot ru
Description:
------------
Array $test soured after use link in foreach.
Test script:
---------------
$test = array(1 => 1, 2 => 2, 3 => 3);
foreach($test as $key => &$value)
{
}
uasort($test, function($a, $b) {
if($a > $b)
return -1;
else if($a > $b)
return 1;
else
return 0;
});
print_r($test);
foreach($test as $key => $value)
{
}
print_r($test);
Expected result:
----------------
Array
(
[3] => 3
[2] => 2
[1] => 1
)
Array
(
[3] => 1
[2] => 2
[1] => 1
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70639&edit=1