首页 > 代码库 > PHP 数组的添加和读取
PHP 数组的添加和读取
在实际的开发中,会经常使用数组的添加和读取。这里把经常使用的操作记下来,以备以后查阅。
1 <?php 2 //一维数值数组 3 $list = array(‘wang‘,‘god‘); 4 $list[] =‘biaobiao‘; 5 $list[] = ‘chaofan‘; 6 foreach($list as $v){ 7 echo $v."<br />"; 8 } 9 10 //一维关联数组 11 $list1 = array( 12 ‘name‘ => ‘god‘, 13 ‘info‘ => ‘XXXXX‘, 14 ‘number‘ => 123213 15 ); 16 $list1[‘address‘] = ‘Beijing‘; 17 foreach($list1 as $key => $value){ 18 echo $key . "=>" . $value."<br />"; 19 } 20 21 22 //二位数值数组 23 $list = array( 24 array(1,4,12,3), 25 array(2,5,6,10), 26 array(3,7,9,8) 27 ); 28 foreach($list as $value){ 29 foreach($value as $v){ 30 echo $v ." "; 31 } 32 echo "<br />"; 33 } 34 35 //二维数组关联数组 36 $lists = array(); 37 $list0 = array( 38 ‘name‘ => ‘god‘, 39 ‘info‘ => ‘XXXXX‘, 40 ‘number‘ => 123213 41 ); 42 $lists[] = $list0; 43 $list1 = array( 44 ‘name‘ => ‘chaofan‘, 45 ‘info‘ => ‘SSSSSS‘, 46 ‘number‘ => 41123 47 ); 48 $lists[] = $list1; 49 $list2 = array( 50 ‘name‘ => ‘biaobiao‘, 51 ‘info‘ => ‘#######‘, 52 ‘number‘ => 56732 53 ); 54 $lists[] = $list2; 55 56 foreach($lists as $key => $value){ 57 echo "name:".$lists[$key][‘name‘]." info:".$lists[$key][‘info‘]." number:".$lists[$key][‘number‘]."<br />"; 58 } 59 60 foreach($lists as $key => $value){ 61 $lists[$key][‘nick‘] = $lists[$key][‘name‘]." host"; 62 } 63 ?>
PHP 数组的添加和读取
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。