<?php #數組的定義方式1 $area = array("beijing","changsha","wuhan"); $areaCode = array("010"=>"beijing","0731"=>"changsha","020"=>"wuhan"); #數組的定義方式2 $student[] = "zhangsan"; $student[] = "lisi"; $student[] = "wangwu"; $stuInfo[] = array("a001"=>"zhangsan"); $stuInfo[] = array("a002"=>"wangwu"); #is_array() echo 'is_array()檢測:'.(is_array($area) == 1 ? "true" : "false"); #foreach array foreach($areaCode as $k => $v){ printf("<br/>%s => %s",$k,$v); } #用於定義值填充數組 $count = range(0, 100); var_dump($count); $t = range("a", "z"); print_r($t); $x = range(0, 100,2); #0-100的偶數 print_r($x); echo "<hr/>"; #添加和刪除數組元素 /* * 1.array_unshift() 在數組頭部添加元素 * 2.array_push() 在數組尾部添加元素 * 3.array_shift() 在數組頭部刪除元素 * 4.array_pop() 在數組尾部刪除元素 * */ array_unshift($area, "shenzhen"); print_r($area); echo "<hr/>"; array_shift($area); print_r($area); echo "<hr/>"; array_push($area, "dongguan"); print_r($area); echo "<hr/>"; array_pop($area); print_r($area); echo "<hr/>"; #定位元組元素 /* * 1.in_array() * 2.array_key_exists() * 3.array_search() * */ echo (in_array("beijing",$area) == 1 ? "true" : "false"); echo "<hr/>"; echo array_key_exists("007", $areaCode) == 1 ? "true" : "false"; echo "<hr/>"; $s = array_search("changsha", $areaCode); if($s){ echo $s.":".$areaCode[$s]; } #擷取數組的Key和Value集合 $ks = array_keys($areaCode); echo "<hr/>"; print_r($ks); $vs = array_values($areaCode); echo "<hr/>"; print_r($vs); #遍曆數組 while($key = key($area)){ printf("<br/>key:%s",$key); next($area); } #向函數傳遞數組值 $cc = array(1,2,3,4,5,6,7); echo "<hr/>"; function Add(&$num) { $num += 10; } array_walk($cc, "Add"); print_r($cc); #計算數組大小 echo "<hr/>"; echo count($area); #數組排序 $r = array_reverse($area); echo "<hr/>"; print_r($r); sort($r); echo "<hr/>"; print_r($r); #數組的合并與串連 #$d = array_merge($a,$b); #array_combine($keys, $values)?>