Home >Backend Development >PHP Tutorial >PHP implements a method to add values ​​to all one-dimensional arrays in a two-dimensional array

PHP implements a method to add values ​​to all one-dimensional arrays in a two-dimensional array

高洛峰
高洛峰Original
2017-02-06 15:52:102718browse

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'] = '欢迎光临PHP中文网';
}
print_r($shop);

Run results:

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);

Running results:

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中文网 )
)

I hope this article will be helpful to everyone in PHP programming.

For more PHP methods to add values ​​to all one-dimensional arrays in a two-dimensional array, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn