首頁  >  文章  >  後端開發  >  php常用函數-陣列 php常用字串函數 php常用類別庫 php常用英文單

php常用函數-陣列 php常用字串函數 php常用類別庫 php常用英文單

WBOY
WBOY原創
2016-07-29 08:50:421084瀏覽

學習php的過程中,整理的一些常用的函數,這是數組函數。

//array():產生一個陣列
$a = array("dog","cat","horse");
print_r($a); //Array ( [0] => dog [1] => cat [2] => horse )
//array_combine():產生一個數組,用一個數組的值作為鍵名,另一個數組的值作為值
$a1 = array("a" ,"b","c","d");
$a2 = array("Cat","Dog","Horse","Cow");
print_r(array_combine($a1,$a2)); //Array ( [a] => Cat [b] => Dog [c] => Horse [d] => Cow )
//range():建立並傳回一個包含指定範圍的元素的陣列。
$number = range(0,50,10); //(0:序列的第一個值;50:序列結束值;10:每次的步長)
print_r ($number); //Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 )
//compact():建立一個由參數所帶變數組成的陣列
$firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$result = compact("firstname" , "lastname", "age");
print_r($result); //Array ( [firstname] => Peter [lastname] => Griffin [age] => 38 )
//array_fill():用給定的值產生陣列
$a = array_fill(2,3,"Dog"); //(2:填衝的第一個鍵值;3:填衝的數值; dog:填衝的內容)
print_r($a); //Array ( [2] => Dog [3] => Dog [4] => Dog )
//array_chunk():把一個陣列分割為新的陣列區塊
$a = array("a"=>"cat","b"=>"dog","c"=>"horse","d"=>"Cow");
print_r(array_chunk ($a,2)); //Array([0] => Array([a]=>cat [b]=>dog) [1] => Array([c]=>horse [d]=> cow))
//array_merge():把兩個數字組合並為一個數組
/***********************與array_combine區別***********************
array_merge( ):直接合併陣列;array_combine():依參數順序第一組為鍵,第二組為值;*/
echo "


";
$a1 = array("a"=>"Horse" ,"b"=>"Dog");
$a2 = array("c"=>"Cow","b"=>"cat");
print_r(array_merge($a1,$a2)); / /Array ( [a] => Horse [b] => Dog [c] => Cow [d] => cat )
//array_diff():傳回兩個陣列的差集(鍵名不變)
$a1 = array(8=>"Cat",1=>"Dog",2=>"Horse",3= >"lion");
$a2 = array(4=>"Horse",5=>"Dog",6=>"bird",7=>"pig");
print_r(array_diff($a1,$ a2)); //Array ( [8] => Cat [3] => lion )
print_r(array_diff($a2,$a1)); //Array ( [6] => bird [7] => pig )
//array_intersect():傳回兩個或多個陣列的交集陣列
$a1 = array(0=>"Cat",1=>"Dog",2=>"Horse");
$a2 = array(3=>"Horse",4=>"Dog",5=>"Fish");
print_r(array_intersect($a1,$a2)); // Array ( [1] => Dog [2] => Horse )
print_r(array_intersect($a2,$a1)); // Array ( [3] => Horse [4] => Dog )
//array_serach在陣列中搜尋給定的值,如果成功則傳回對應的鍵名(失敗回傳false)
$a = array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
echo array_search("Dog ",$a); //a
//array_slice():在數組中根據條件取出一段值,並返回(鍵名保持不變)
echo "
";
$a = array(" a"=>"Dog","b"=>"Cat","c"=>"Horse","d"=>"Bird");
print_r(array_slice($a,1,2)); //1:從鍵值(相當於索引鍵為1的位置開始取);2,取兩個
//Array ( [b] => Cat [c] => Horse )
//array_splice():把陣列中的一部分去掉,並用其它的值取代
$a1 = array(4=>"Dog",'b'=>"Cat",'c'=>"Horse",6=>"Bird") ;
$a2 = array(3=>"Tiger",5=>"Lion");
array_splice($a1,1,2,$a2);
/* $a1:被替換的陣列(最後被輸出的陣列);1:按索引鍵的1位置開始替換;2:替換兩個;$a2:替換數組,加到$a1中去*/
print_r($a1); //Array ( [0] = > Dog [1] => Tiger [2] => Lion [3] => Bird )
//array_splice($a1,0,2,$a2);
//print_r($a1); // Array ( [0] => Tiger [1] => Lion [2] => Horse [3] => Bird )
//array_sum():計算數組中所有值的和
$a = array(0=>"5 ",1=>"15",2=>"25");
echo array_sum($a); //45
//in_array():檢查陣列中是否存在某個值
$animals = array(" dog", "cat", "cow", "horse");
if (in_array("cow",$animals)){
echo "Match found";
}else{
echo "Match not found";
}
//array_key_exists():檢查給定的鍵名是否存在於陣列中( 參數1:鍵名參數2:陣列):傳回bool值
$animals = array("a"=>"dog", "b "=>"cat", "c"=>"cow", "d"=>"horse","d"=>"lion");
echo array_key_exists("a",$animals); //1沒有不回傳false值
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
} //The 'first' element is in the array
/* 陣列指標操作*/
//key():傳回陣列內部指標目前指向元素的鍵名稱
//current():傳回陣列目前的元素
//next():把指向目前元素的指標移到下一個元素的位置,並傳回目前元素的值
//prev():把指向目前元素的指標移到上一個元素的位置,並傳回目前元素的值
//end():把目前內部指標指向最後一個元素,並傳回該元素的值
//reset():把陣列元素指標指向第一個值,並傳回這個元素的值
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array).'
';
}
next($array);
} //fruit1 fruit4 fruit5
/* 遍歷數組*/
/*正向遍歷*/
$a = array(10,20,30);
reset($a) ;
do{
echo key($a)."==>".current($a)."
";
}while(next($a)); // 0==>10 1 ==>20 2==>30
/*倒向遍歷*/
end($a);
do{
echo key($a)."===>".current($a)."";
}while(prev($a)); //2===>30 1===>20 0===>10
/* 指標*/
$transport = array('foot', 'bike', ' car', 'plane');
/*預設第一個為目前元素*/
$mode = current($transport); // $mode = 'foot';
$mode = next($transport); / / $mode = 'bike';
/*當前元素為'bike'*/
$mode = current($transport); // $mode = 'bike';
$mode = prev($transport); // $mode = 'foot';
$mode = end($transport); // $mode = 'plane';
$mode = current($transport); // $mode = 'plane';
//list( ):把數組中的值賦給一些變數---------list不是函數
$arr = array("Cat","Dog","Horse","Cow");
list($ a,$b,$c,$d) = $arr;
echo $a; //Cat
echo $b; //Dog
echo $c; //Horse
echo $d; //Cow
// array_shift():刪除陣列中的第一個元素,並傳回被刪除元素的值
$a = array("1"=>​​"Dog","2"=>"Cat","3"=>" Horse");
echo array_shift($a); //Dog
print_r ($a); //Array ( [b] => Cat [c] => Horse )
//array_unshift():在陣列開關插入一個或多個元素(如果當前數組為索引數組,則從0開始,依此類推;關聯數組鍵名不變)
$a = array("10"=>"Cat","b"=>" Dog",3=>"horse",5=>"lion");
array_unshift($a,"Horse");
print_r($a); //Array ( [0] => Horse [1] = > Cat [b] => Dog [2] => horse [3] => lion )
//array_push():向數組最後壓入一個或多個元素
$a=array("a"=>"Dog","3"=>"Cat");
array_push($a,"Horse ","Bird");
print_r($a); //Array ( [a] => Dog [3] => Cat [4] => Horse [5] => Bird )
//array_pop():刪除陣列中的最後一個元素
$a=array("Dog","Cat","Horse");
array_pop($a);
print_r($a); //Array ( [0] => Dog [1] => Cat )
/* 數組鍵值操作*/
//shuffle():將數組打亂,鍵名為索引數組從0開始(不能直接打印shuffle,分開寫)
$animals = array ("a"=>"dog", "b"=>"cat", "c"=>"cow", "d"=>"horse","d"=>"lion");
shuffle( $animals);
print_r($animals); //Array ( [0] => dog [1] => cow [2] => cat [3] => lion ) 每刷新一次會隨機變化
//count ():計算數組中的單元數目式物件中的屬性個數
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo count($people); //4
//array_flip():傳回一個鍵值反轉後的陣列
$a = array(0=>"Dog",1=>"Cat",2=>"Horse");
print_r(array_flip($ a)); //Array ( [Dog] => 0 [Cat] => 1 [Horse] => 2 )
//array_keys():傳回陣列所有的鍵,組成一個陣列
$a = array(" a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_keys($a)); //Array ( [0] => a [1] => b [2] => c )
//array_values():傳回陣列中所有的值,組成一個陣列
$a = array("a"=>"Horse","b"=>"Cat" ,"c"=>"Dog");
print_r(array_values($a)); //Array ( [0] => Horse [1] => Cat [2] => Dog )
//array_reverse() :傳回一個元素順序相反的陣列
$a = array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_reverse($a) ); //Array ( [c] => Dog [b] => Cat [a] => Horse )
//array_count_values():統計數組中所有值出現的次數
$a = array(1,2, 3,4,1,1,3,5,3,2,1,3,4);
print_r(array_count_values($a)); //Array ( [1] => 4 [2] => 2 [ 3] => 4 [4] => 2 [5] => 1 )
//array_rand():從陣列中隨機抽取一個或多個元素,注意是鍵名
$a=array("a"= >"Dog","b"=>"Cat","c"=>"Horse","d"=>"Lion","e"=>"Cow");
print_r(array_rand($a, 3)); //Array ( [0] => b [1] => c [2] => e ) ***隨機***
//each():傳回數組中目前的鍵/值對並將陣列指標往後移一步
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);
print_r($bar); //Array ( [1] => bob [value] => bob [0] => 0 [key] => 0 )
/*每遍歷一次,指針向後移動一位*/
$bar = each($foo);
print_r($bar); //Array ( [1] => fred [value] => fred [0] => 1 [key] => 1 )
//array_unique():刪除重複值,傳回剩餘備份
$a =array("a"=>"狗","b"=>"貓","c"=>"馬","d"=>"狗","e"=>"牛", "f"=>"牛");
print_r(array_unique($a)); //數組( [a] => 狗[b] => 貓[c] => 馬[e] => 牛[f ] => 牛 )
/* 吞吐量排序*/
/**
* 傳回值為1(正值):表示交換順序
* 傳回值為-1(負值):表示不交換順序
**/
/**
* //原始鍵名被忽略(從零開始)(字串順序)
* sort():對值由小到大
* rsort():對值由大到小
*
* //原始鍵名保留(字串順序)
* asort():對值從小到大
* arsort():對值由大到小
**/
$my_array = array("a" => "Dog", "b" => "Cat" , "c" => "馬");
sort($my_array);
print_r($my_array); //Array ( [0] => Cat [1] => Dog [2] => Horse )
$my_array = array("a" => "Dog", "b" => "Cat ", "c" => "馬");
asort($my_array);
print_r($my_array); //Array ( [ b] => Cat [a] => Dog [c] => Horse )
/**
* ksort():對下標由小到大
* krsort():對下標由大到小
**/
$my_array = array("h" => "Dog" , "s" => "貓" , "a" => "馬");
ksort($my_array);
print_r($my_array); //Array ( [a] => Horse [h] => Dog [s] => Cat )
$ my_array = array("e" => "Dog", "2" => "Cat ", "a" => "Horse");//按什麼順序排序
ksort($my_array);
print_r($my_array ); //陣列( [a] => 馬[e] => 狗[2] => 貓)
/**
* usort():使用使用者自訂的回呼函數對陣列排序
* uasort():使用使用者自訂的回呼函數對陣列排序並保持索引關聯
* uksort():使用使用者自訂的回呼函數對數組排序對數組鍵排序
**/
$v = array(1,3,5,2,4 ) ;
usort($v,'fun');
函數fun($v1,$v2){
return ( $v1 > $v2 ) ? 1 : -1;
}
print_r($v); //Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
/* 排序加瀏覽*/
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a }
$a = array(3, 2, 5, 6,
}
$a = array(3, 2, 5, 6,
}
$a = array(3, 2, 5, 6,
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
foreach ($a as $key => $value) {
echo $key."==>".$value." "; //0== =>1 1===>2 2===>3 3===>5 4===>6
}
/* 排序遍歷結束*/
/* *
* sort():由小到大的字串排序(字母相等,比較不相等之後的一位大小)
* natsort();由小到大的自然排序(字母相等,比較數值)***區分大小寫
* natcasesort():不區分大小寫的自然排序**/

$a = array("a" => "id2", "b" => "id12", "c" => "id22","d" => "ID22 ");

排序($a);列印_r( $a); //陣列( [0] => ID22 [1] => id12 [2] => id2 [3] => id22 )

natsort($a);印出_r($a); //陣列( [0] => ID22 [2] => id2 [1] => id12 [3] => id22 )

natcasesort($a) ;印出_r($a); //陣列( [2] => id2 [1] => id12 [3] => id22 [0] => ID22 )

🎜🎜 🎜 以上就介紹了php常用函數-資料庫,包括了php、常用方面的內容,希望對PHP教程有興趣的朋友有幫助。 🎜 🎜 🎜
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn