php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39114 Foreach loop acces original array
Submitted: 2006-10-10 13:30 UTC Modified: 2006-10-10 14:00 UTC
From: perso at cyprien dot org Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.1.6 OS: Debian stable
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: perso at cyprien dot org
New email:
PHP Version: OS:

 

 [2006-10-10 13:30 UTC] perso at cyprien dot org
Description:
------------
Foreach loop access original array when using references on a previous loop.

Reproduce code:
---------------
<?php

$list = array( 1, 2, 3, 4 );

$new_list = array();

foreach( $list as $item ){
        $z =& $new_list[];
        $z = $item;
}

//unset( $z );

print_r( $new_list );

foreach( $new_list as $z ){
}

print_r( $new_list );



?>


Expected result:
----------------
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Actual result:
--------------
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 3
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-10 13:43 UTC] tony2001@php.net
Duplicate of bug #29992
 [2006-10-10 13:57 UTC] perso at cyprien dot org
yes, duplicate. sorry.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 12:01:31 2025 UTC