php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67916 Reference does not work as expected in array
Submitted: 2014-08-27 14:44 UTC Modified: 2014-08-27 16:22 UTC
From: qerazor at googlemail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: Irrelevant OS: Ubuntu
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: qerazor at googlemail dot com
New email:
PHP Version: OS:

 

 [2014-08-27 14:44 UTC] qerazor at googlemail dot com
Description:
------------
PHP Version 5.5.9-1ubuntu4.1 but am not sure if that maters in this case.

one of the foreach takes the value as ref. the other foreach does not. as you can see in the result of the script, the dump of this array after manipulating after the first foreach is correct. but the comparison within the second foreach is wrong.

to get that resolved i have to rename one of the $value variables or use the $value variable in the second foreach also as ref.

Test script:
---------------
$array = array(
    'a'=> 'foo',
    'b'=> 'foo',
    'c'=> 'foo'
);

var_dump($array);
foreach($array as &$value) {
    $value = false;
}
var_dump($array);
$array['b'] = true;

var_dump($array);
foreach($array as $key => $value) {
    echo $key.' = '.((int)$value).'<br>';
}

Expected result:
----------------
array (size=3)
  'a' => string 'foo' (length=3)
  'b' => string 'foo' (length=3)
  'c' => string 'foo' (length=3)

array (size=3)
  'a' => boolean false
  'b' => boolean false
  'c' => boolean false

array (size=3)
  'a' => boolean false
  'b' => boolean true
  'c' => boolean false

a = 0
b = 1
c = 0

Actual result:
--------------
array (size=3)
  'a' => string 'foo' (length=3)
  'b' => string 'foo' (length=3)
  'c' => string 'foo' (length=3)

array (size=3)
  'a' => boolean false
  'b' => boolean false
  'c' => &boolean false

array (size=3)
  'a' => boolean false
  'b' => boolean true
  'c' => &boolean false

a = 0
b = 1
c = 1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-08-27 16:22 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-08-27 16:22 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

After the first foreach $value is still a reference to the last element in $array. When the next foreach begins you reuse $value and that will copy each value you find in the array into that last element.

unset($value) after the first foreach.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Dec 02 01:01:24 2020 UTC