2015-02-08 112 views
0

我有這段代碼但不起作用。我如何修改foreach循環內的數組?

$arr1 = array('test1', 'test2', 'test3'); 
$arr2 = array(1, 2, 3); 

foreach ($arr1 as $x) { 
    $m = array_shift($arr2); 

    //at this point, $arr2 remains unchanged, why? 

    continue; 
} 

$ arr2在迭代內部和之間似乎沒有變化。任何見解將不勝感激。謝謝!

+0

的[?改變foreach循環內的初始陣列]可能重複(http://stackoverflow.com/questions/2348077/change-initial -array-內最的foreach環) – VeeeneX 2015-02-08 12:12:24

回答

0

這是行不通的。這裏該代碼:

$arr1 = array('test1', 'test2', 'test3'); 
$arr2 = array(1, 2, 3); 

foreach ($arr1 as $x) { 
    $m = array_shift($arr2); 

    print_r($arr2); 

    continue; 
} 

將打印:

Array ([0] => 2 [1] => 3) Array ([0] => 3) Array () 
相關問題