dtvjl64442
dtvjl64442
2014-07-28 17:24

foreach不能正确打印更改的数组

  • foreach
  • php
  • arrays
已采纳

I am trying to print an array using foreach and while printing, if a certain $key comes up, I want to make changes to the array. Problem is, even though the array gets changed, the changes do not get printed.

In the example below, you will find:

  • function I use to change the array;
  • an array first printed with no changed;
  • then echo print-out in with changes during the process - all using foreach;
  • another print-out of the same table, but this time with changes.

    <?php
    
    function insert_before_key($array, $key, $data = NULL){
        if (($offset = array_search($key, array_keys($array))) === false){
            $offset = count($array); 
        }
        return array_merge(array_slice($array, 0, $offset), (array) $data, array_slice($array, $offset));
    }
    
    $array = array(
        "no_color"  =>  "blank",
        "color1"    =>  "red",
        "color2"    =>  "green",
        "color3"    =>  "blue",
        );
    
    echo "<pre>";
    print_r($array);
    echo "</pre>";
    
    foreach ($array as $key => $value) {
        echo $key . ": " . $value . "<br />";
        if ($key === "color1"){
            $array = insert_before_key($array, "color2", array("color1.5" => "yellow"));
        }
    }
    
    echo "<pre>";
    print_r($array);
    echo "</pre>";
    
    echo "<br />";
    
    ?>
    

Note that the new $key is to jump in AFTER current $key, so I would expect it to come up. Any idea why this happens ?

EDIT:

Played a bit more with foreach and I think it must be caching the keys or something...

    <?php
    $test_array = array(0,1,2,3,4,5,6,7,8,9);

    foreach ($test_array as $key => $value) {
        if ($key === 5){$test_array[7] = $test_array[7]+1;}
        echo $key . ": " . $value . "<br />";
    }

    print_r($test_array);
    ?>

The above will display UNCHANGED echo, but CHANGED print_r.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • douhuanglou1445 douhuanglou1445 7年前

    From the manual: "As foreach relies on the internal array pointer, changing it within the loop may lead to unexpected behavior." http://php.net/manual/en/control-structures.foreach.php

    You shouldn't modify an array you're looping over.

    点赞 评论 复制链接分享
  • dongshuming7131 dongshuming7131 7年前

    I'd keep it simple. Something tells me however that you're fixing effects of a problem here and not its source.

    $array = array(
        "no_color"  =>  "blank",
        "color1"    =>  "red",
        "color2"    =>  "green",
        "color3"    =>  "blue",
    );
    
    $temp_array = array();
    
    foreach ($array as $key => $value) {
        $temp_array[$key] = $value;
        echo $key . ": " . $value . "<br />";
        if ($key == 'color1') {
            $key_add = 'color1.5';
            $value_add = 'yellow';
            $temp_array[$key_add] = $value_add;
            echo $key_add . ": " . $value_add . "<br />";
        }
    }
    
    $array = $temp_array;
    
    点赞 评论 复制链接分享
  • douyou9923 douyou9923 7年前

    So during iteration you are trying to change the value of the item being iterated

    foreach($array ...)
    {
        change $array
    }
    

    Use a copy of $array inside the iteration

     $array2 = $array
     foreach($array ...)
    {
        change $array2
    }
    
    点赞 评论 复制链接分享

为你推荐