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

From: Date: Wed, 10 Dec 2014 18:41:40 +0000
Subject: Bug #68586 [Opn->Nab]: 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-188998@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 Updated by: requinix@php.net Reported by: hmdiana at gmail dot com Summary: Variable losing memory address when used by function that passes by reference -Status: Open +Status: Not a bug Type: Bug Package: Variables related Operating System: All PHP Version: 5.4.35 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 http://php.net/manual/en/control-structures.foreach.php "In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference." Previous Comments: ------------------------------------------------------------------------ [2014-12-10 18:26:59] hmdiana at gmail dot com I selected wrong package for the bug... changed that ------------------------------------------------------------------------ [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 (#188998) next »