程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php 數組的常用函數

php 數組的常用函數

編輯:關於PHP編程

 

在php教程中數組是種強大的數據類型,他可以做的事情很多,可以存儲不同的數據類型在一個數組中,下面我們列出了數組常用的操作,排序,鍵名對數組排序等做法。

/* 數組的常用函數
 *
 * 數組的排序函數
 *   sort()
 *   rsort()
 *   usort()
 *   asort()
 *   arsort()
 *   uasort()
 *   ksort()
 *   krsort()
 *   uksort()
 *   uatsort()
 *   natcasesort()
 *   array_multisort()
 *
 *    1.簡單的數組排序
 *      sort() rsort()
 *    2.根據鍵名對數組排序
 *      ksort() krsort()
 *    3.根據元素的值對數組排序
 *      asort() arsort()
 *    4.根據"自然數排序“法對數組排序
 *      natsort()//區分大小寫字母比較 natcasescort()//不區分大小寫字母的比較
 *    5.根據用戶自定義規則對數組排序
 *      usort() uasort() uksort()對鍵排序
 *    6.對維數組的排序
 *      array_multisort()
 *
 * 拆分、合並、分解、接合的數組函數
 *    1.array_slice()
 *    2.array_splice()//刪除
 *    3.array_combine()//合並
 *    4.array_merge();//合並
 *    5.array_intersect();//多個數組的交集
 *    6.array_diff();//返回多個數組的差集
 *
 * 數組與數據結構的函數
 *   1.使用數組實現堆棧 //先進後出
 *    array_push() array_pop()
 *   2.使用數組實現隊列 //先進先出
 *    array_unshift() array_shift() unset()
 *
 * 
 * 其他與數組操作有關的函數
 *    array_rand()
 *    shuffle()
 *    array_sum()
 *    range()
 */

//簡單數組排序的使用
$data=array(5,8,1,7,2);
sort($data);//元素由小到大進行排序
print_r($data);//Array ( [0] => 1 [1] => 2 [2] => 5 [3] => 7 [4] => 8 )
rsort($data);//元素由大到小進行排序
print_r($data);//Array ( [0] => 8 [1] => 7 [2] => 5 [3] => 2 [4] => 1 )

//根據鍵名排序的例子
$data_2=array(5=>"five",8=>"eight",1=>"one",7=>"seven",2=>"two");
ksort($data_2);//對數組的下標進行由小到大排序
print_r($data_2);//Array ( [1] => one [2] => two [5] => five [7] => seven [8] => eight )
krsort($data_2);//對數組的下標進行由大到小排序
print_r($data_2);//Array ( [8] => eight [7] => seven [5] => five [2] => two [1] => one )

 

//根據元素的值對數組排序
$data_3=array("1"=>"Linux","a"=>"Apache","m"=>"MySQL","l"=>"PHP");
//asort() arsort  與 sort() rsort()的區別在於 前者排序後保持原有的鍵名,後者不保持原有鍵名,且鍵名從0開始
asort($data_3);
print_r($data_3);//Array ( [a] => Apache [1] => Linux [m] => MySQL [l] => PHP )
echo '
';
arsort($data_3);
print_r($data_3);//Array ( [l] => PHP [m] => MySQL [1] => Linux [a] => Apache )
echo '
';
sort($data_3);
print_r($data_3);//Array ( [0] => Apache [1] => Linux [2] => MySQL [3] => PHP )
echo '
';
rsort($data_3);
print_r($data_3);//Array ( [0] => PHP [1] => MySQL [2] => Linux [3] => Apache )

//根據”自然數排序法“對數組排序(0-9短者優先)
$data_4=array("file.txt","file11.txt","file2.txt","file22.txt");
sort($data_4);
print_r($data_4);//Array ( [0] => file.txt [1] => file11.txt [2] => file2.txt [3] => file22.txt )
echo '
';
natsort($data_4);
print_r($data_4);//Array ( [0] => file.txt [2] => file2.txt [1] => file11.txt [3] => file22.txt )
echo '
';
natcasesort($data_4);
print_r($data_4);//Array ( [0] => file.txt [2] => file2.txt [1] => file11.txt [3] => file22.txt )
echo '
';

//用戶自定義排序函數
echo '
';
$data_5=array("Linux","Apache","MySQL","PHP");
usort($data_5,"sortbylen");//通過元素長度排序
print_r($data_5);//Array ( [0] => PHP [1] => MySQL [2] => Linux [3] => Apache )
function sortbylen($one,$two){
 if(strlen($one)==strlen($two))
  return 0;
 else
  return (strlen($one)>strlen($two))?1:-1;
}

//拆分、合並、分解、接合的數組函數
echo '
';
$data_6=array("Linux","Apache","MySQL","PHP");
print_r(array_slice($data_6,1,2));//取下標為1、2的元素
//Array ( [0] => Apache [1] => MySQL ) 下標重置從0開始
echo '
';

print_r(array_slice($data_6,-2,1));//從後邊的第二個開始取返回一個,不是從0開始的
//Array ( [0] => MySQL ) 下標重置從0開始
echo '
';

print_r(array_slice($data_6,1,2,true));
//Array ( [1] => Apache [2] => MySQL )  保留原有的下標

echo '
';


//array_combine()
$a1=array("OS","WebServer","DataBase","Language");
$a2=array("Linux","Apache","MySQL","PHP");

print_r(array_combine($a1,$a2));//第一個參數作為鍵名,第二個作為值來合並
//Array ( [OS] => Linux [WebServer] => Apache [DataBase] => MySQL [Language] => PHP )

echo '
';

//array_merge()
$a3=array("OS","WebServer","DataBase","Language");
$a4=array("Linux","Apache","MySQL","PHP");
$a5=$a3+$a4;
print_r($a5);//因為兩個數組下標重復所以顯示這樣
//Array ( [0] => OS [1] => WebServer [2] => DataBase [3] => Language )
echo '
';
print_r(array_merge($a3,$a4));//合並並重新索引
//Array ( [0] => OS [1] => WebServer [2] => DataBase [3] => Language [4] => Linux [5] => Apache [6] => MySQL [7] => PHP )

echo '
';

//array_intersect()
$a7=array("OS","WebServer","DataBase","Language",1,2,3);
$a8=array("Linux","Apache","MySQL","PHP",2,3,4);
print_r(array_intersect($a7,$a8));//Array ( [5] => 2 [6] => 3 )
echo '
';

//array_diff()
$a9=array(1,2,3,4);
$a10=array(3,4,5,6);
print_r(array_diff($a9,$a10));//Array ( [0] => 1 [1] => 2 )
//返回第一個數組跟第二個相差的元素
echo '
';


//使用數組實現堆棧
$b=array(1,2,3,4);
$b[]="a";//入棧
array_push($b,"b","c");//使用函數入棧
print_r($b);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => a [5] => b [6] => c )
echo '
';

$value=array_pop($b);//使用函數出棧
print_r($b);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => a [5] => b )
echo '
';
echo $value;//顯示出棧的元素的值 c
echo '
';

//使用數組實現隊列
$c=array(1,2,3);
print_r($c);//Array ( [0] => 1 [1] => 2 [2] => 3 )
echo '
';
array_unshift($c,"abc","bcd");//入隊
print_r($c);//Array ( [0] => abc [1] => bcd [2] => 1 [3] => 2 [4] => 3 )
echo '
';
$values=array_shift($c);//出隊
print_r($c);// Array ( [0] => bcd [1] => 1 [2] => 2 [3] => 3 )
echo '
';
unset($c[2]);//刪除指定位置元素
print_r($c);//Array ( [0] => bcd [1] => 1 [3] => 3 )
echo '
';


//array_rand()  隨機返回數組下標
$arr=array(1,3,4,5,76,7,99,6,2,3);
echo array_rand($arr);//返回的是隨機的數組元素的下標
echo $arr[array_rand($arr)];//隨機顯示數組元素的值
echo '
';
//shuffle()  隨機重新排列數組
$arr2=array(32,35,33);
shuffle($arr2);
print_r($arr2);//數組元素位置隨機變換
echo '
';
//array_sum()  求和
$arr3=array(1,3,5);
echo array_sum($arr3); //返回9
echo '
';
print_r($arr3);//Array ( [0] => 1 [1] => 3 [2] => 5 )
echo '
';
//range(最小值,最大值,步長)
$arr4=range(0,100,10);
print_r($arr4);//Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 [6] => 60 [7] => 70 [8] => 80 [9] => 90 [10] => 100 )

?>


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved