欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php实现给二维数组中所有一维数组添加值的方法

程序员文章站 2022-03-27 14:21:27
...
本文实例讲述了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中文网 )
)

希望本文所述对大家PHP程序设计有所帮助。

更多php实现给二维数组中所有一维数组添加值的方法相关文章请关注PHP中文网!