<?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) ?>