在实际的开发中,会经常使用数组的添加和读取。这里把经常使用的操作记下来,以备以后查阅。
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 ?>