本文執行個體講述了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'] = '歡迎光臨topic.alibabacloud.com';}print_r($shop);
運行結果:
Array ([0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 4444444444444 [we] => 歡迎光臨topic.alibabacloud.com )[1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 歡迎光臨topic.alibabacloud.com )[2] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 歡迎光臨topic.alibabacloud.com ))
//樣本2:修改迴圈變數數組,重新賦值foreach($shop as $key=>$shoplist){ $index = count($shoplist); $shoplist[$index] = '4444444444444'; $shoplist['we'] = '歡迎光臨topic.alibabacloud.com'; $shop[$key]=$shoplist;}print_r($shop);
運行結果:
Array ([0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 4444444444444 [we] => 歡迎光臨topic.alibabacloud.com )[1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 歡迎光臨topic.alibabacloud.com )[2] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 歡迎光臨topic.alibabacloud.com ))
希望本文所述對大家PHP程式設計有所協助。