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

yipeiwu_com6年前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 number_format() 函数定义和用法

number_format() 函数通过千位分组来格式化数字。 语法 number_format(number,decimals,decimalpoint,separator)...

php中使用__autoload()自动加载未定义类的实现代码

下面是一段使用__autoload()的代码,供大家学习参考: 复制代码 代码如下:<?php/*** 自动加载相关类库文件*/function __autoload($class...

php创建多级目录的方法

本文实例讲述了php创建多级目录的方法。分享给大家供大家参考。具体实现方法如下: <?php /* 写出一个能创建多级目录的PHP函数 */ function...

PHP实现算式验证码和汉字验证码实例

在PHP网站开发中,验证码可以有效地保护我们的表单不被恶意提交,但是如果不使用算式验证码或者汉字验证码,仅仅使用简单的字母或者数字验证码,这样的验证码方案真的安全吗? 大家知道简单数字或...

解析php中call_user_func_array的作用

一、直接调用方法复制代码 代码如下:function test($a, $b) {echo '测试一:'.$a.$b;}//调用test方法,array("asp", 'php')对应相...