//數組的創建 //1 $usernames = array('李彥宏','周宏偉','馬雲','俞敏洪','李開復'); echo $usernames; //array,打印類型 echo '<br />'; echo $usernames[1]; echo '<br />'; print_r($usernames);//print_r()打印變量的信息 //2 通過range()函數自動創建一個數組 $numbers = range(1, 10); $letters = range('a', 'z'); $numbers2 = range(1, 10, 2); //第三個參數表示步長 print_r($numbers); echo '<br />'; print_r($letters); echo '<br />'; print_r($numbers2); //1 3 5 7 9 //循環打印數組 //for循環,count()獲得數組長度,這種必須下標從0開始,且為數字 for($i=0;$i<count($numbers);$i++){ echo '<br />'; echo $numbers[$i]; } //foreach()打印數組,只能打印數組 if(is_array($numbers)){ //如果是數組 foreach ($numbers as $key=>$value){ echo '<br />'; echo $key.'---'.$value; } } //3 自定義鍵數組 $usernames = array('baisu'=>'李彥宏','taobao'=>'馬雲','tengxu'=>'馬化騰'); // print_r($usernamessernames); $usernames['goggle'] = '李開復'; //追加數組 // print_r($usernames); //直接創建自定義鍵數組 $userages['黨'] = '22'; $userages['周'] = '23'; print_r($userages); // 循環打印foreach();each();list(); foreach ($userages as $key=>$value){ echo '<br />'; echo $key."--".$value; } // $a = each($userages); //返回數組當前鍵值對(是一個數組)並將數組指針向前移動一步 // echo $a[0].'--'.$a[1]; while (!!$a=each($userages)){ //!!轉化為布爾值,如果值存在返回1 echo $a[0].'--'.$a[1].'<br />'; //echo $a['key'].'--'.$a['value'].'<br />'; } // $a = array('a','b','c'); //list()只能識別數字下標 // list($var1,$var2)=$a; //list($var1,$var2)就是將$var1='a',$var2='b' // echo $var2; //b list($name,$age)=each($userages); echo $name.'---'.$age.'<br />'; //reset();將數組的指針指向第一個單元上,可以對each()指針操作 //$a = array_unique();創建一個新數組,新數組移除原數組中重復的值,原數組不動 //array_flip();對調數組中的鍵和值,不會改變本體,而是返回新數組,同上 //4 數組里的數組:二維數組 $products=array( array('蘋果',1), array('橘子',2), array('栗子',3) ); // print_r($products); //雙重循環打印數組 // echo count($products); //3 for ($i=0;$i<count($products);++$i){ for ($j=0;$j<count($products[$i]);++$j){ echo $products[$i][$j].'|'; } echo '<br />'; } //雙重循環打印二維數組 $products=array( array('產品名'=>'蘋果','數量'=>1), array('產品名'=>'蘋果','數量'=>1), array('產品名'=>'蘋果','數量'=>1) ); for ($i=0;$i<count($products);++$i){ foreach ($products[$i] as $key=>$value){ echo $key.'--'.$value.'|'; } echo '<br />'; } for ($i=0;$i<count($products);++$i){ while (!!list($key,$value)=each($products[$i])){ echo $key.'--'.$value.'|'; } echo '<br />'; } //5 數組排序 $fruit = array('banner','orange','apple'); //sort(); sort($fruit); print_r($fruit); //a b o,中文也可以的 $numbers = array(2,3,4,5,6); sort($numbers); print_r($numbers); //sort()函數的第二個參數是可選的。這個可選參數可以傳遞SORT_REGULAR(默認值)、SORT_NUMERIC或SORT_STRING。指定排序類型的功能是非常有用的。比如,當要比較可能包含有數字2和12的字符串時,從數字角度看,2要小於12,但是作為字符串,"12"卻要小於"2"。 $numbers = array("12","2"); sort($numbers,SORT_STRING);//按字符串排序 print_r($numbers); //asort(),保留索引關系 $fruit = array('banner','orange','apple'); asort($fruit); print_r($fruit); //ksort(),對鍵排序 $fruit = array('b'=>'banner','a'=>'orange','c'=>'apple'); ksort($fruit); print_r($fruit); //反向排序,在前邊的前邊加r $numbers = array(2,3,4,5,6); rsort($numbers); print_r($numbers); shuffle();隨機排序 $pic = array('mm1.jpg','mm2.jpg','mm3.jpg','mm4.jpg','mm5.jpg'); shuffle($pic); //隨機打亂數組 for ($i=0;$i<count($pic)-2;++$i){ echo '<img src="images/'.$pic[$i].'" style="margin:10px;" />'; echo "\n"; } //把數組反方向,一般array開頭的不會改變原數組,生成新的數組 $a = array_reverse($pic); print_r($a); //6 添加函數 $username = array("dang"); //array_unshift();開頭插入元素,插入成功返回int array_unshift($username, 'zhou'); print_r($username); //array_push();//結尾插入數據 array_push($username,'haha'); print_r($username); // 刪除數組元素 array_shift($array); 刪除開頭元素 array_pop();刪除結尾元素 array_rand();隨機取出數組中一個或多個鍵,多個返回的是數組 $fruit = array('banner','orange','apple'); $a = array_rand($fruit,1); echo $fruit[$a]; //7 數組指針的操作 // 在數組中瀏覽:each()、current()、reset()、end()、next()、pos()、prev(); // 調用next()或each()將使指針前移一個元素。調用each($array_name)會在指針前移一個位置之前返回當前元素。next()函數則有些不同----調用next($array_name)是將指針前移,然后再返回新的當前元素。 // 要反向遍歷一個數組,可以使用end()和prev()函數。prev()函數和next()函數相反。它是將當前指針往回移一個位置然后再返回新的當前元素。 $fruit = array('banner','orange','apple'); $usernames = array('baisu'=>'李彥宏','taobao'=>'馬雲','tengxu'=>'馬化騰'); //current();獲取指針的當前元素,current並沒有把指針移到下一步 echo current($usernames); //next();向后移一位 echo next($usernames); echo current($usernames); reset($usernames); //將指針指向第一個單元 echo current($usernames); $usernames = array('baisu'=>'李彥宏','taobao'=>'馬雲','tengxu'=>'馬化騰'); //統計數組個數,sizeof();count(); echo sizeof($usernames); echo count($usernames); //array_count_values()統計數組內所有值出現的個數 $a = array(1,2,3,3,3,2,2); print_r (array_count_values($a)); //8 將數組轉換成標量變量 //對於一個非數字索引數組,而該數組又有許多關鍵字-值對,可以使用函數extract()將它們轉換成一系列的標量變量。 //函數extract()的作用是通過一個數組創建一系列的標量變量,這些變量的名稱必須是數組中關鍵字的名稱,而變量值則是數組中的值。 //extract(array var_array,[int extract_type],[string prefix]); $fruits = array('a'=>'apple','b'=>'banner','c'=>'orange'); extract($fruits); echo $a; //apple echo $c; //orange