#40654 [NEW]: reference causes foreach to fail proper iteration

Ask a Question related to PHP Bugs, Design and Development.

  1. #1

    Default #40654 [NEW]: reference causes foreach to fail proper iteration

    From: saeven at saeven dot net
    Operating system:
    PHP version: 5.2.1
    PHP Bug Type: Scripting Engine problem
    Bug description: reference causes foreach to fail proper iteration

    Description:
    ------------
    Using a reference to build an array, later causes foreach to fail when
    reusing the reference variable name.

    The bug is solved by adding unset( $element); before block B, but this
    shouldn't be necessary.

    Reproduce code:
    ---------------
    // Block A - Build the Array
    $a = array( 2 => array( "value" => "a" ),
    4 => array( "value" => "b" ),
    6 => array( "value" => "c" ) );

    $b = array( "orange", "blue", "green" );

    foreach( $a as $id => &$element ){
    $extra = array();

    foreach( $b as $color )
    array_push( $extra, $color );

    $element += array( 'extra' => $extra );
    }

    // Block B - Iterate through
    foreach( $a as $element )
    echo $element["value"].' ';


    Expected result:
    ----------------
    a b c

    Actual result:
    --------------
    a b b

    --
    Edit bug report at [url]http://bugs.php.net/?id=40654&edit=1[/url]
    --
    Try a CVS snapshot (PHP 4.4): [url]http://bugs.php.net/fix.php?id=40654&r=trysnapshot44[/url]
    Try a CVS snapshot (PHP 5.2): [url]http://bugs.php.net/fix.php?id=40654&r=trysnapshot52[/url]
    Try a CVS snapshot (PHP 6.0): [url]http://bugs.php.net/fix.php?id=40654&r=trysnapshot60[/url]
    Fixed in CVS: [url]http://bugs.php.net/fix.php?id=40654&r=fixedcvs[/url]
    Fixed in release: [url]http://bugs.php.net/fix.php?id=40654&r=alreadyfixed[/url]
    Need backtrace: [url]http://bugs.php.net/fix.php?id=40654&r=needtrace[/url]
    Need Reproduce Script: [url]http://bugs.php.net/fix.php?id=40654&r=needscript[/url]
    Try newer version: [url]http://bugs.php.net/fix.php?id=40654&r=oldversion[/url]
    Not developer issue: [url]http://bugs.php.net/fix.php?id=40654&r=support[/url]
    Expected behavior: [url]http://bugs.php.net/fix.php?id=40654&r=notwrong[/url]
    Not enough info: [url]http://bugs.php.net/fix.php?id=40654&r=notenoughinfo[/url]
    Submitted twice: [url]http://bugs.php.net/fix.php?id=40654&r=submittedtwice[/url]
    register_globals: [url]http://bugs.php.net/fix.php?id=40654&r=globals[/url]
    PHP 3 support discontinued: [url]http://bugs.php.net/fix.php?id=40654&r=php3[/url]
    Daylight Savings: [url]http://bugs.php.net/fix.php?id=40654&r=dst[/url]
    IIS Stability: [url]http://bugs.php.net/fix.php?id=40654&r=isapi[/url]
    Install GNU Sed: [url]http://bugs.php.net/fix.php?id=40654&r=gnused[/url]
    Floating point limitations: [url]http://bugs.php.net/fix.php?id=40654&r=float[/url]
    No Zend Extensions: [url]http://bugs.php.net/fix.php?id=40654&r=nozend[/url]
    MySQL Configuration Error: [url]http://bugs.php.net/fix.php?id=40654&r=mysqlcfg[/url]
    saeven at saeven dot net Guest

  2. Similar Questions and Discussions

    1. #35106 [Asn->Csd]: nested foreach fails when array variable has a reference
      ID: 35106 Updated by: dmitry@php.net Reported By: olympnn at nm dot ru -Status: Assigned +Status: ...
    2. #39307 [NEW]: foreach call by reference BUG
      From: lolitka at gmail dot com Operating system: Windows XP PHP version: 5.1.6 PHP Bug Type: Unknown/Other Function Bug...
    3. Proper way to "foreach" through a two dimensional array?
      Hi folks, I seem to be getting "Illegal offset type" errors. Here is a portion of the array values: $FormVarsAry = "FirstName";...
    4. #8373 [Com]: foreach by reference
      ID: 8373 Comment by: giles_m_allen at yahoo dot co dot uk Reported By: ocomte at guarantycity dot com Status: ...
    5. How to bail from an iteration but not the program?...
      All- How can I terminate the running of a program from within an iteration, but not leave the program? EG, In this code: ...
  3. #2

    Default #40654 [Com]: reference causes foreach to fail proper iteration

    ID: 40654
    Comment by: same at saeven dot net
    Reported By: saeven at saeven dot net
    Status: Open
    Bug Type: Scripting Engine problem
    PHP Version: 5.2.1
    New Comment:

    I'd add that print_r properly displays the constructed array, which
    contradicts what foreach displays (which is why I thought this required
    filing).


    Previous Comments:
    ------------------------------------------------------------------------

    [2007-02-27 04:44:35] saeven at saeven dot net

    Description:
    ------------
    Using a reference to build an array, later causes foreach to fail when
    reusing the reference variable name.

    The bug is solved by adding unset( $element); before block B, but this
    shouldn't be necessary.

    Reproduce code:
    ---------------
    // Block A - Build the Array
    $a = array( 2 => array( "value" => "a" ),
    4 => array( "value" => "b" ),
    6 => array( "value" => "c" ) );

    $b = array( "orange", "blue", "green" );

    foreach( $a as $id => &$element ){
    $extra = array();

    foreach( $b as $color )
    array_push( $extra, $color );

    $element += array( 'extra' => $extra );
    }

    // Block B - Iterate through
    foreach( $a as $element )
    echo $element["value"].' ';


    Expected result:
    ----------------
    a b c

    Actual result:
    --------------
    a b b


    ------------------------------------------------------------------------


    --
    Edit this bug report at [url]http://bugs.php.net/?id=40654&edit=1[/url]
    same at saeven dot net Guest

  4. #3

    Default #40654 [Opn->Bgs]: reference causes foreach to fail proper iteration

    ID: 40654
    Updated by: [email]tony2001@php.net[/email]
    Reported By: saeven at saeven dot net
    -Status: Open
    +Status: Bogus
    Bug Type: Scripting Engine problem
    PHP Version: 5.2.1
    New Comment:

    Duplicate of bug #29992.


    Previous Comments:
    ------------------------------------------------------------------------

    [2007-02-27 05:00:30] same at saeven dot net

    I'd add that print_r properly displays the constructed array, which
    contradicts what foreach displays (which is why I thought this required
    filing).

    ------------------------------------------------------------------------

    [2007-02-27 04:44:35] saeven at saeven dot net

    Description:
    ------------
    Using a reference to build an array, later causes foreach to fail when
    reusing the reference variable name.

    The bug is solved by adding unset( $element); before block B, but this
    shouldn't be necessary.

    Reproduce code:
    ---------------
    // Block A - Build the Array
    $a = array( 2 => array( "value" => "a" ),
    4 => array( "value" => "b" ),
    6 => array( "value" => "c" ) );

    $b = array( "orange", "blue", "green" );

    foreach( $a as $id => &$element ){
    $extra = array();

    foreach( $b as $color )
    array_push( $extra, $color );

    $element += array( 'extra' => $extra );
    }

    // Block B - Iterate through
    foreach( $a as $element )
    echo $element["value"].' ';


    Expected result:
    ----------------
    a b c

    Actual result:
    --------------
    a b b


    ------------------------------------------------------------------------


    --
    Edit this bug report at [url]http://bugs.php.net/?id=40654&edit=1[/url]
    tony2001@php.net Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139