php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68586 Variable losing memory address when used by function that passes by reference
Submitted: 2014-12-10 18:21 UTC Modified: 2014-12-10 18:41 UTC
From: hmdiana at gmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.4.35 OS: All
Private report: No CVE-ID: None
 [2014-12-10 18:21 UTC] 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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [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
I selected wrong package for the bug... changed that
 [2014-12-10 18:41 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-12-10 18:41 UTC] requinix@php.net
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."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC