Home > Article > Backend Development > PHP implements adding values to all one-dimensional arrays in a two-dimensional array
This article mainly introduces the method of PHP to add values to all one-dimensional arrays in a two-dimensional array, involving PHP's array traversal, conversion, assignment and other related operating skills. Friends who need it can refer to it
The example of this article describes how PHP implements adding values to all one-dimensional arrays in a two-dimensional array. Share it with everyone for your reference, the details are as follows:
Add values (indexes and associations) to all one-dimensional arrays in the two-dimensional array
$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'] = '欢迎光临脚本之家'; } print_r($shop);
Running results:
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 4444444444444 [we] => 欢迎光临脚本之家 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临脚本之家 ) [2] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临脚本之家 ) )
//示例2:修改循环变量数组,重新赋值 foreach($shop as $key=>$shoplist){ $index = count($shoplist); $shoplist[$index] = '4444444444444'; $shoplist['we'] = '欢迎光临脚本之家'; $shop[$key]=$shoplist; } print_r($shop);
Running results:
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 4444444444444 [we] => 欢迎光临脚本之家 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临脚本之家 ) [2] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临脚本之家 ) )
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
How to add values to all one-dimensional arrays in two-dimensional arrays in php
##php Detailed explanation of the method of array implementation to merge the same key values to generate a new two-dimensional array based on a certain key value
The above is the detailed content of PHP implements adding values to all one-dimensional arrays in a two-dimensional array. For more information, please follow other related articles on the PHP Chinese website!