在實際的開發中,會常用數組的添加和讀取。這裏把常用的操做記下來,以備之後查閱。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 ?>