|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2014-12-10 18:26 UTC] hmdiana at gmail dot com
-Package: Scripting Engine problem
+Package: Variables related
[2014-12-10 18:26 UTC] hmdiana at gmail dot com
[2014-12-10 18:41 UTC] requinix@php.net
-Status: Open
+Status: Not a bug
[2014-12-10 18:41 UTC] requinix@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 08:00:01 2025 UTC |
Description: ------------ When inside a foreach and using a function that should get the variable passed by reference, that function changes the memory address of that variable so,instead of working with the original variable, it works with a copy of it and the original variable never get the changes we made. Test script: --------------- $things = array( 909 => array( 'name' => 'Colors', 'children' => array( 0 => array('name' => 'Yellow', 'id' => 855), 1 => array('name' => 'Blue', 'id' => 842), 2 => array('name' => 'Brown', 'id' => 843), 3 => array('name' => 'Red', 'id' => 856) ) ) ); foreach ($things as $thing) { if (isset($thing['children'])) { array_multisort($thing['children']); } } Expected result: ---------------- The array 'children' on alphabetical order, ordered by 'name': array (size=10) 909 => array (size=4) 'name' => string 'Colors' (length=9) 'children' => array (size=14) 0 => array (size=2) 'name' => string 'Blue' (length=9) 'id' => string '842' (length=3) 1 => array (size=2) 'name' => string 'Brown' (length=8) 'id' => string '843' (length=3) 2 => array (size=2) 'name' => string 'Red' (length=5) 'id' => string '856' (length=3) 3 => array (size=2) 'name' => string 'Yellow' (length=5) 'id' => string '855' (length=3) Actual result: -------------- The initial variable, as if nothing has changed: array (size=10) 909 => array (size=4) 'name' => string 'Colors' (length=9) 'children' => array (size=14) 0 => array (size=2) 'name' => string 'Yellow' (length=9) 'id' => string '855' (length=3) 1 => array (size=2) 'name' => string 'Blue' (length=8) 'id' => string '842' (length=3) 2 => array (size=2) 'name' => string 'Brown' (length=5) 'id' => string '843' (length=3) 3 => array (size=2) 'name' => string 'Red' (length=5) 'id' => string '856' (length=3)