Bug #70639 [Opn->Nab]: Error use link in foreach

From: Date: Mon, 05 Oct 2015 16:08:13 +0000
Subject: Bug #70639 [Opn->Nab]: Error use link in foreach
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-196409@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70639&edit=1 ID: 70639 Updated by: stas@php.net Reported by: alex-dev at yandex dot ru Summary: Error use link in foreach -Status: Open +Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: Windows XP, Free BSD PHP Version: 5.4.45 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Previous Comments: ------------------------------------------------------------------------ [2015-10-05 09:52:31] inefedor at gmail dot com 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. ------------------------------------------------------------------------ [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

« previous php.bugs (#196409) next »