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

yipeiwu_com5年前PHP代码库

本文实例讲述了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'] = '欢迎光临【宜配屋www.yipeiwu.com】';
}
print_r($shop);

运行结果:

Array (
[0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 4444444444444 [we] => 欢迎光临【宜配屋www.yipeiwu.com】 )
[1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临【宜配屋www.yipeiwu.com】 )
[2] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临【宜配屋www.yipeiwu.com】 )
)

//示例2:修改循环变量数组,重新赋值
foreach($shop as $key=>$shoplist){
  $index = count($shoplist);
  $shoplist[$index] = '4444444444444';
  $shoplist['we'] = '欢迎光临【宜配屋www.yipeiwu.com】';
  $shop[$key]=$shoplist;
}
print_r($shop);

运行结果:

Array (
[0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 4444444444444 [we] => 欢迎光临【宜配屋www.yipeiwu.com】 )
[1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临【宜配屋www.yipeiwu.com】 )
[2] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4444444444444 [we] => 欢迎光临【宜配屋www.yipeiwu.com】 )
)

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

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

相关文章

PHP比较运算符的详细介绍

比较运算符种类 如同它们名称所暗示的,允许对两个值进行比较。比较运算符有如下几个: 1) $a > $b 大于:如果 $a 严格大于$b,则返回TRUE 2) $a < $b...

php中get_defined_constants函数用法实例分析

本文实例讲述了php中get_defined_constants函数用法。分享给大家供大家参考。具体分析如下: get_defined_constants ( PHP 4中 > =...

ajax php 实现写入数据库

首先需要一个带输入表格. 复制代码 代码如下:<!-- To change this template, choose Tools | Templates and open the...

10个简化PHP开发的工具

10个简化PHP开发的工具

本文介绍了可以帮助简化 PHP 开发的10个项目,包括框架,类库,工具,代码。 1.CakePHP Development Framework CakePHP 是一个 PHP 的快速开...

PHP时间戳和日期相互转换操作实例小结

本文实例总结了PHP时间戳和日期相互转换操作。分享给大家供大家参考,具体如下: 在php中我们要把时间戳转换日期可以直接使用date函数来实现,如果要把日期转换成时间戳可以使用strto...