Bug #68586 [Opn]: Variable losing memory address when used by function that passes by reference

From: Date: Wed, 10 Dec 2014 18:27:00 +0000
Subject: Bug #68586 [Opn]: Variable losing memory address when used by function that passes by reference
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188997@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68586&edit=1 ID: 68586 User updated by: hmdiana at gmail dot com Reported by: hmdiana at gmail dot com Summary: Variable losing memory address when used by function that passes by reference Status: Open Type: Bug -Package: Scripting Engine problem +Package: Variables related Operating System: All PHP Version: 5.4.35 Block user comment: N Private report: N New Comment: I selected wrong package for the bug... changed that Previous Comments: ------------------------------------------------------------------------ [2014-12-10 18:21:24] hmdiana at gmail dot com 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) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68586&edit=1

« previous php.bugs (#188997) next »