dynamically changing array in foreach

后端 未结 3 1635
你的背包
你的背包 2021-01-27 04:19

is it possible somehow change array which is processed by foreach? I tried this script

$iterator = 10;
$cat = array(1 => \'a\',2 => \'b\',3 => \'c\');

         


        
3条回答
  •  迷失自我
    2021-01-27 04:37

    The internet needs more cats! pass the array by reference instead and you'll get the desired result. note the &$cat in the loop.

    $iterator = 10;
    $cat = array(1 => 'a',2 => 'b',3 => 'c');
    
    foreach($cat as $k => &$c)
    {
        if ($iterator < 15)
        {
            $cat[$iterator] = $iterator;
            $iterator++;
        }
        echo $c;  
    }
    

提交回复
热议问题