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
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 10 22:01:30 2024 UTC