本文實例講述了php實作給二維數組中所有一維數組添加值的方法。分享給大家參考,具體如下:
給二維數組中所有的一維數組添加值(索引和關聯)
$shop = array( 0=>array(0=>1,1=>2,2=>3,3=>4) ,1=>array(0=>1,1=>2,2=>3) ,2=>array(0=>1,1=>2,2=>3) ); print_r($shop); //示例 1:引用循环变量的地址赋值 foreach($shop as &$shoplist){ $shoplist[] = '4444444444444'; $shoplist['we'] = '欢迎光临PHP中文网'; } print_r($shop);
運行結果:
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 4444444444444 [we] => 欢迎光临PHP中文网 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临PHP中文网 ) [2] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临PHP中文网 ) )
//示例2:修改循环变量数组,重新赋值 foreach($shop as $key=>$shoplist){ $index = count($shoplist); $shoplist[$index] = '4444444444444'; $shoplist['we'] = '欢迎光临PHP中文网'; $shop[$key]=$shoplist; } print_r($shop);
運行結果:
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 4444444444444 [we] => 欢迎光临PHP中文网 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临PHP中文网 ) [2] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临PHP中文网 ) )