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'] = '欢迎光临脚本之家'; } print_r($shop);

运行结果:

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

运行结果:

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] => 欢迎光临脚本之家 ) )

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php排序算法总结》、《PHP常用遍历算法与技巧总结》、《PHP数学运算技巧总结》及《php常见数据库操作技巧汇总

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/25bf1633d52e519a50a7394736d33996.html