range($min,$max[$step=1])
:快速建立下標連續的索引數組range(開始, 結束, 步長)php
range('a','z'); // 從a到z, 26個字母, 索引數組
複製代碼
compact($varname,$varname....)
:快速建立關聯數組<?php
$username = "zhangsan";
$sex = "male";
$age = 18;
$arr = compact('username','sex','age');
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun.php:10:
array (size=3)
'username' => string 'zhangsan' (length=8)
'sex' => string 'male' (length=4)
'age' => int 18
複製代碼
array_fill($start_index,$num,$value)
:用給定的值填充數組<?php
$arr1 = array_fill(3,4,'blue');
var_dump($arr1);
複製代碼
D:\php20190701\php_code\0809\array_fun.php:7:
array (size=4)
3 => string 'blue' (length=4)
4 => string 'blue' (length=4)
5 => string 'blue' (length=4)
6 => string 'blue' (length=4)
複製代碼
array_fill_keys($keys,$value)
:使用指定的鍵和值填充數組<?php
$keys = array('a','b','c');
$arr = array_fill_keys($keys, 'blue');
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun.php:7:
array (size=3)
'a' => string 'blue' (length=4)
'b' => string 'blue' (length=4)
'c' => string 'blue' (length=4)
複製代碼
<?php
$keys = array('a','b','c');
$values = array('aaa','bbb','ccc');
$arr = array_fill_keys($keys, $values);
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun.php:9:
array (size=3)
'a' =>
array (size=3)
0 => string 'aaa' (length=3)
1 => string 'bbb' (length=3)
2 => string 'ccc' (length=3)
'b' =>
array (size=3)
0 => string 'aaa' (length=3)
1 => string 'bbb' (length=3)
2 => string 'ccc' (length=3)
'c' =>
array (size=3)
0 => string 'aaa' (length=3)
1 => string 'bbb' (length=3)
2 => string 'ccc' (length=3)
複製代碼
array_combine($keys,$values)
:建立一個數組,用一個數組的值做爲其鍵名, 另外一個數組的值, 做爲鍵值<?php
$keys = array('a','b','c');
$values = array('aaa','bbb','ccc');
$arr = array_combine($keys, $values);
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun.php:9:
array (size=3)
'a' => string 'aaa' (length=3)
'b' => string 'bbb' (length=3)
'c' => string 'ccc' (length=3)
複製代碼
count($var[,$mode=COUNT_NORMAL])/sizeof()
:計算數組中的元素數量array_keys($array)
:取得數組的鍵名做爲下標連續的索引數組
返回===
, false 至關於 ==
, 默認是false<?php
$arr = ['Volvo'=>'XC90',"BMW"=>"X5","Toyota"=>"Highlander"];
var_dump(array_keys($arr));
複製代碼
D:\php20190701\php_code\0809\array_fun.php:5:
array (size=3)
0 => string 'Volvo' (length=5)
1 => string 'BMW' (length=3)
2 => string 'Toyota' (length=6)
複製代碼
<?php
$arr = ['Volvo'=>'XC90',"BMW"=>"5","Toyota"=>5];
var_dump(array_keys($arr,5,true)); // 5 和 "5" 是不同的
var_dump(array_keys($arr,5,false)); // 5 和 "5" 同樣
var_dump(array_keys($arr,5)); // 默認false
複製代碼
D:\php20190701\php_code\0809\array_fun.php:5:
array (size=1)
0 => string 'Toyota' (length=6)
D:\php20190701\php_code\0809\array_fun.php:6:
array (size=2)
0 => string 'BMW' (length=3)
1 => string 'Toyota' (length=6)
D:\php20190701\php_code\0809\array_fun.php:7:
array (size=2)
0 => string 'BMW' (length=3)
1 => string 'Toyota' (length=6)
複製代碼
array_values($array)
:取得數組的鍵值做爲下標連續的索引數組返回<?php
$arr = ['Volvo'=>'XC90',"BMW"=>"X5","Toyota"=>"Highlander"];
var_dump(array_values($arr));
複製代碼
D:\php20190701\php_code\0809\array_fun.php:5:
array (size=3)
0 => string 'XC90' (length=4)
1 => string 'X5' (length=2)
2 => string 'Highlander' (length=10)
複製代碼
array_flip($array)
:交換數組中的鍵名和鍵值<?php
$arr1 = ['first'=>1,'second'=>2,'third'=>3];
var_dump(array_flip($arr1)); // 鍵名和鍵值互換
複製代碼
D:\php20190701\php_code\0809\array_fun.php:7:
array (size=3)
1 => string 'first' (length=5)
2 => string 'second' (length=6)
3 => string 'third' (length=5)
複製代碼
in_array($search,$array[,$strict])
:檢測數組中是否存在某個值<?php
$arr = ['Volvo'=>'XC90',"BMW"=>"X5","Toyota"=>"Highlander"];
var_dump(in_array("X5",$arr)); // true
var_dump(in_array("X6",$arr)); // false
var_dump(in_array("BMW",$arr)); // false, 只能搜索鍵值
複製代碼
D:\php20190701\php_code\0809\array_fun.php:5:boolean true
D:\php20190701\php_code\0809\array_fun.php:6:boolean false
D:\php20190701\php_code\0809\array_fun.php:7:boolean false
複製代碼
<?php
$arr = ['Volvo'=>'XC90',"BMW"=>"5","Toyota"=>"5"];
var_dump(in_array(5,$arr,true)); // 至關於 "==="
var_dump(in_array(5,$arr,false)); // 至關於 "=="
var_dump(in_array(5,$arr)); // 默認false
複製代碼
D:\php20190701\php_code\0809\array_fun.php:5:boolean false
D:\php20190701\php_code\0809\array_fun.php:6:boolean true
D:\php20190701\php_code\0809\array_fun.php:7:boolean true
複製代碼
array_search($search,$array[,$strict])
:在數組中搜索給定的值<?php
$arr = ['a'=>'red','b'=>'green','c'=>'blue'];
echo array_search('red', $arr);
複製代碼
a
複製代碼
<?php
$arr = ['a'=>'red','b'=>'green','c'=>'blue','d'=>"5"];
var_dump(array_search(5, $arr,true)); // 至關於 "==="
var_dump(array_search(5, $arr,false)); // 至關於 "=="
var_dump(array_search(5, $arr)); // 默認false
複製代碼
D:\php20190701\php_code\0809\array_fun.php:5:boolean false
D:\php20190701\php_code\0809\array_fun.php:6:string 'd' (length=1)
D:\php20190701\php_code\0809\array_fun.php:7:string 'd' (length=1)
複製代碼
<?php
$arr = ['a'=>'red','b'=>'green','c'=>"5",'d'=>5];
var_dump(array_search(5, $arr,true)); // d
var_dump(array_search(5, $arr,false)); // c
var_dump(array_search(5, $arr)); // c
複製代碼
D:\php20190701\php_code\0809\array_fun.php:5:string 'd' (length=1)
D:\php20190701\php_code\0809\array_fun.php:6:string 'c' (length=1)
D:\php20190701\php_code\0809\array_fun.php:7:string 'c' (length=1)
複製代碼
array_key_exists($search,$array)
:檢查給定的鍵名或索引是否存在於數組中<?php
$arr = [
'first'=>'one',
'second'=>'two',
'third'=>'three',
];
var_dump(array_key_exists('first',$arr));
var_dump(array_key_exists('first111',$arr));
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:10:boolean true
D:\php20190701\php_code\0809\array_fun3.php:11:boolean false
複製代碼
<?php
$arr = ['one','two','three'];
var_dump(array_key_exists('0',$arr)); // true
var_dump(array_key_exists(0,$arr)); // true
var_dump(array_key_exists('1',$arr)); // true
var_dump(array_key_exists(1,$arr)); // true
複製代碼
array_reverse($array[,$preserve_keys=false])
:數組倒置<?php
$arr = ['one','two','three'];
var_dump(array_reverse($arr));
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:6:
array (size=3)
0 => string 'three' (length=5)
1 => string 'two' (length=3)
2 => string 'one' (length=3)
D:\php20190701\php_code\0809\array_fun3.php:7:
array (size=3)
0 => string 'one' (length=3)
1 => string 'two' (length=3)
2 => string 'three' (length=5)
複製代碼
<?php
$arr = ['one','two','three'];
var_dump(array_reverse($arr));
var_dump(array_reverse($arr,false));
var_dump(array_reverse($arr,true));
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:6:
array (size=3)
0 => string 'three' (length=5)
1 => string 'two' (length=3)
2 => string 'one' (length=3)
D:\php20190701\php_code\0809\array_fun3.php:7:
array (size=3)
0 => string 'three' (length=5)
1 => string 'two' (length=3)
2 => string 'one' (length=3)
D:\php20190701\php_code\0809\array_fun3.php:8:
array (size=3)
2 => string 'three' (length=5)
1 => string 'two' (length=3)
0 => string 'one' (length=3)
複製代碼
shuffle($array)
:打亂數組的元素<?php
$arr = ['one','two','three','four','five','six','seven'];
var_dump(shuffle($arr)); // true
var_dump($arr); // 展現結果
var_dump(shuffle($arr)); // true
var_dump($arr); // 展現結果
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:6:boolean true
D:\php20190701\php_code\0809\array_fun3.php:7:
array (size=7)
0 => string 'three' (length=5)
1 => string 'five' (length=4)
2 => string 'seven' (length=5)
3 => string 'two' (length=3)
4 => string 'six' (length=3)
5 => string 'four' (length=4)
6 => string 'one' (length=3)
D:\php20190701\php_code\0809\array_fun3.php:8:boolean true
D:\php20190701\php_code\0809\array_fun3.php:9:
array (size=7)
0 => string 'six' (length=3)
1 => string 'seven' (length=5)
2 => string 'three' (length=5)
3 => string 'one' (length=3)
4 => string 'four' (length=4)
5 => string 'two' (length=3)
6 => string 'five' (length=4)
複製代碼
<?php
$arr = [
'1st'=>'one',
'2nd'=>'two',
'3rd'=>'three',
'4th'=>'four',
'5th'=>'five',
'6th'=>'six',
'7th'=>'seven',
];
var_dump(shuffle($arr)); // true
var_dump($arr); // 展現結果
var_dump(shuffle($arr)); // true
var_dump($arr); // 展現結果
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:14:boolean true
D:\php20190701\php_code\0809\array_fun3.php:15:
array (size=7)
0 => string 'three' (length=5)
1 => string 'one' (length=3)
2 => string 'two' (length=3)
3 => string 'five' (length=4)
4 => string 'four' (length=4)
5 => string 'six' (length=3)
6 => string 'seven' (length=5)
D:\php20190701\php_code\0809\array_fun3.php:16:boolean true
D:\php20190701\php_code\0809\array_fun3.php:17:
array (size=7)
0 => string 'seven' (length=5)
1 => string 'four' (length=4)
2 => string 'three' (length=5)
3 => string 'two' (length=3)
4 => string 'six' (length=3)
5 => string 'one' (length=3)
6 => string 'five' (length=4)
複製代碼
array_unique($array[,$sort_flag=SORT_STRING])
:移除數組中重複的值<?php
$arr = [
'seven',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
];
var_dump(array_unique($arr));
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:15:
array (size=7)
0 => string 'seven' (length=5)
1 => string 'one' (length=3)
2 => string 'two' (length=3)
3 => string 'three' (length=5)
4 => string 'four' (length=4)
5 => string 'five' (length=4)
6 => string 'six' (length=3)
D:\php20190701\php_code\0809\array_fun3.php:16:
array (size=8)
0 => string 'seven' (length=5)
1 => string 'one' (length=3)
2 => string 'two' (length=3)
3 => string 'three' (length=5)
4 => string 'four' (length=4)
5 => string 'five' (length=4)
6 => string 'six' (length=3)
7 => string 'seven' (length=5)
複製代碼
<?php
$arr = [
"111"=>'seven',
"222"=>'one',
"333"=>'two',
"444"=>'three',
"555"=>'four',
"666"=>'five',
"777"=>'six',
"888"=>'seven',
];
var_dump(array_unique($arr));
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:15:
array (size=7)
111 => string 'seven' (length=5)
222 => string 'one' (length=3)
333 => string 'two' (length=3)
444 => string 'three' (length=5)
555 => string 'four' (length=4)
666 => string 'five' (length=4)
777 => string 'six' (length=3)
D:\php20190701\php_code\0809\array_fun3.php:16:
array (size=8)
111 => string 'seven' (length=5)
222 => string 'one' (length=3)
333 => string 'two' (length=3)
444 => string 'three' (length=5)
555 => string 'four' (length=4)
666 => string 'five' (length=4)
777 => string 'six' (length=3)
888 => string 'seven' (length=5)
複製代碼
<?php
$arr = [
123,'123a',123
];
var_dump(array_unique($arr,SORT_STRING));
var_dump(array_unique($arr,SORT_NUMERIC));
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:8:
array (size=2)
0 => int 123
1 => string '123a' (length=4)
D:\php20190701\php_code\0809\array_fun3.php:9:
array (size=1)
0 => int 123
複製代碼
array_rand($array[,$num_req=1])
:隨機取出數組的鍵名<?php
$arr1 = ['first'=>1,'second'=>2,'third'=>3];
// 隨機返回鍵名, 是一個數組
// 第二個參數若是不寫, 默認是1, 返回就不是數組, 而是數字或者字符串(鍵名)
// 第二個參數若是是0, 或者超出數組數量, 報warning, 返回null
var_dump(array_rand($arr1,2));
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:8:
array (size=2)
0 => string 'first' (length=5)
1 => string 'third' (length=5)
複製代碼
array_sum($array)
:統計數組中元素值的總和<?php
$str = "1,2,5,6,9";
$arr = explode(',',$str);
var_dump(array_sum($arr)); // 23
複製代碼
<?php
$arr = ["a1","2b","5c","6d","9e"];
var_dump(array_sum($arr)); // 22
複製代碼
array_product($array)
:計算數組中全部值的乘積$arr = [1,2,3,4];
var_dump(array_product($arr)); // 24
複製代碼
<?php
$arr = ["1a",'11.1',"5c"];
var_dump(array_product($arr)); // 55.5
複製代碼
array_count_values($array)
:統計數組中值出現的次數<?php
$arr = ['red','red','green','orange','grass','orange','grey','blue','grass','pink'];
var_dump(array_count_values($arr));
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:5:
array (size=7)
'red' => int 2
'green' => int 1
'orange' => int 2
'grass' => int 2
'grey' => int 1
'blue' => int 1
'pink' => int 1
複製代碼
extract($array[,$extract_type=EXTR_OVERWRITE[,$prefix=null]])
:數組轉變量鍵名轉成變量名, 鍵值轉成變量值算法
若是重名, 會覆蓋以前的變量segmentfault
第二個參數: 出現重複時的處理方案數組
第三個參數: 前綴, 加載變量名前面, 自動增長_
bash
prefix
。prefix
。<?php
$arr = [
"a"=>'red',
"b"=>'red',
"c"=>'green'
];
extract($arr);
var_dump($a);
var_dump($b);
var_dump($c);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:11:string 'red' (length=3)
D:\php20190701\php_code\0809\array_fun3.php:12:string 'red' (length=3)
D:\php20190701\php_code\0809\array_fun3.php:13:string 'green' (length=5)
複製代碼
<?php
$a = "abc";
$arr = [
"a"=>'red',
"b"=>'red',
"c"=>'green'
];
extract($arr,EXTR_PREFIX_ALL,"pre");
var_dump($a);
var_dump($pre_a);
var_dump($pre_b);
var_dump($pre_c);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:13:string 'abc' (length=3)
D:\php20190701\php_code\0809\array_fun3.php:14:string 'red' (length=3)
D:\php20190701\php_code\0809\array_fun3.php:15:string 'red' (length=3)
D:\php20190701\php_code\0809\array_fun3.php:16:string 'green' (length=5)
複製代碼
array_pad($array,$size,$value)
:用值將數組填補到指定長度<?php
$arr = ['red','green'];
var_dump(array_pad($arr,5,'blue'));
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:4:
array (size=5)
0 => string 'red' (length=3)
1 => string 'green' (length=5)
2 => string 'blue' (length=4)
3 => string 'blue' (length=4)
4 => string 'blue' (length=4)
複製代碼
<?php
$arr = ['red','green'];
var_dump(array_pad($arr,5,['blue','red','pink']));
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:4:
array (size=5)
0 => string 'red' (length=3)
1 => string 'green' (length=5)
2 =>
array (size=3)
0 => string 'blue' (length=4)
1 => string 'red' (length=3)
2 => string 'pink' (length=4)
3 =>
array (size=3)
0 => string 'blue' (length=4)
1 => string 'red' (length=3)
2 => string 'pink' (length=4)
4 =>
array (size=3)
0 => string 'blue' (length=4)
1 => string 'red' (length=3)
2 => string 'pink' (length=4)
複製代碼
key($array)
:獲得當前指針所在位置元素的鍵名<?php
$arr = ['red','green','blue','pink','orange'];
var_dump(key($arr)); // 0
複製代碼
current($array)|pos($array)
:獲得當前指針所在位置元素的鍵值<?php
$arr = ['red','green','blue','pink','orange'];
var_dump(current($arr)); // red
複製代碼
next($array)
:將數組指針向下移動一位,而且返回當前指針所在位置元素的鍵值<?php
$arr = ['red','green','blue','pink','orange'];
var_dump(next($arr)); // green
複製代碼
prev($array)
:將數組指針向上移動一位,而且返回當前指針所在位置元素的鍵值<?php
$arr = ['red','green','blue','pink','orange'];
var_dump(next($arr)); // green
複製代碼
end($array)
:將數組指針移動倒數組的末尾,而且返回當前指針所在位置元素的鍵值<?php
$arr = ['red','green','blue','pink','orange'];
var_dump(end($arr)); // orange
複製代碼
reset($array)
:將數組指針移動到數組的開始,而且返回當前指針所在位置元素的鍵值<?php
$arr = ['red','green','blue','pink','orange'];
var_dump(reset($arr)); // red
複製代碼
each($array)
:返回數組中當前的鍵值對,並將數組指針向下移動一位<?php
$arr = ['red','green','blue','pink','orange'];
var_dump(each($arr));
var_dump(each($arr));
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:4:
array (size=4)
1 => string 'red' (length=3)
'value' => string 'red' (length=3)
0 => int 0
'key' => int 0
D:\php20190701\php_code\0809\array_fun3.php:5:
array (size=4)
1 => string 'green' (length=5)
'value' => string 'green' (length=5)
0 => int 1
'key' => int 1
複製代碼
list($var,...)
:將數組中元素的值賦給對應的變量<?php
$arr = ['red','green','blue','pink','orange'];
list($a,$b,$c,$d,$e) = $arr;
var_dump($a,$b,$c,$d,$e);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:5:string 'red' (length=3)
D:\php20190701\php_code\0809\array_fun3.php:5:string 'green' (length=5)
D:\php20190701\php_code\0809\array_fun3.php:5:string 'blue' (length=4)
D:\php20190701\php_code\0809\array_fun3.php:5:string 'pink' (length=4)
D:\php20190701\php_code\0809\array_fun3.php:5:string 'orange' (length=6)
複製代碼
array_unshift($array,$value...)
:在數組開頭插入一個元素或者多個元素<?php
$arr = ['red','green','blue','pink','orange'];
array_unshift($arr,'hello');
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:5:
array (size=6)
0 => string 'hello' (length=5)
1 => string 'red' (length=3)
2 => string 'green' (length=5)
3 => string 'blue' (length=4)
4 => string 'pink' (length=4)
5 => string 'orange' (length=6)
複製代碼
array_shift($array)
:彈出數組的第一個元素<?php
$arr = ['red','green','blue','pink','orange'];
array_shift($arr);
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:5:
array (size=4)
0 => string 'green' (length=5)
1 => string 'blue' (length=4)
2 => string 'pink' (length=4)
3 => string 'orange' (length=6)
複製代碼
array_push($array,$value...)
:在數組末尾壓入一個元素或者多個元素<?php
$arr = ['red','green','blue','pink','orange'];
array_push($arr,'hello');
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:5:
array (size=6)
0 => string 'red' (length=3)
1 => string 'green' (length=5)
2 => string 'blue' (length=4)
3 => string 'pink' (length=4)
4 => string 'orange' (length=6)
5 => string 'hello' (length=5)
複製代碼
array_pop($array)
:彈出數組的最後一個元素<?php
$arr = ['red','green','blue','pink','orange'];
array_pop($arr);
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:5:
array (size=4)
0 => string 'red' (length=3)
1 => string 'green' (length=5)
2 => string 'blue' (length=4)
3 => string 'pink' (length=4)
複製代碼
sort($array[,$sort_flag=SORT_REGULAR])
:對數組的鍵值按照升序排列,不保留鍵名<?php
$arr = ["Volvo","BMW","Toyota"];
var_dump(sort($arr));
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun.php:5:boolean true
D:\php20190701\php_code\0809\array_fun.php:7:
array (size=3)
0 => string 'BMW' (length=3)
1 => string 'Toyota' (length=6)
2 => string 'Volvo' (length=5)
複製代碼
rsort($array[,$sort_flag=SORT_REGULAR])
:對數組鍵值按照降序排列,不保留鍵名<?php
$arr = ["Volvo","BMW","Toyota"];
var_dump(rsort($arr));
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun.php:5:boolean true
D:\php20190701\php_code\0809\array_fun.php:7:
array (size=3)
0 => string 'Volvo' (length=5)
1 => string 'Toyota' (length=6)
2 => string 'BMW' (length=3)
複製代碼
asort($array[,$sort_flag=SORT_REGULAR])
:對數組鍵值按照升序排列,保留鍵值對關係<?php
$arr = [
"Joe"=>"43",
"Peter"=>"35",
"Ben"=>"37",
];
var_dump(asort($arr));
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun2.php:9:boolean true
D:\php20190701\php_code\0809\array_fun2.php:11:
array (size=3)
'Peter' => string '35' (length=2)
'Ben' => string '37' (length=2)
'Joe' => string '43' (length=2)
複製代碼
arsort($array[,$sort_flag=SORT_REGULAR])
:對數組鍵值按照降序排列,保留鍵值對關係<?php
$arr = [
"Joe"=>"43",
"Peter"=>"35",
"Ben"=>"37",
];
var_dump(arsort($arr));
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun2.php:9:boolean true
D:\php20190701\php_code\0809\array_fun2.php:11:
array (size=3)
'Joe' => string '43' (length=2)
'Ben' => string '37' (length=2)
'Peter' => string '35' (length=2)
複製代碼
ksort($array[,$sort_flag=SORT_REGULAR])
:對數組的鍵名按照升序排列<?php
$arr = [
"Joe"=>"43",
"Peter"=>"35",
"Ben"=>"37",
];
var_dump(ksort($arr));
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun2.php:9:boolean true
D:\php20190701\php_code\0809\array_fun2.php:11:
array (size=3)
'Ben' => string '37' (length=2)
'Joe' => string '43' (length=2)
'Peter' => string '35' (length=2)
複製代碼
krsort($array[,$sort_flag=SORT_REGULAR])
:對數組的鍵名按照降序排列<?php
$arr = [
"Joe"=>"43",
"Peter"=>"35",
"Ben"=>"37",
];
var_dump(krsort($arr));
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun2.php:9:boolean true
D:\php20190701\php_code\0809\array_fun2.php:11:
array (size=3)
'Peter' => string '35' (length=2)
'Joe' => string '43' (length=2)
'Ben' => string '37' (length=2)
複製代碼
natsort($array)
:用天然排序法排序<?php
$arr = [
"tmp15.txt",
"tmp10.txt",
"tmp1.txt",
"tmp22.txt",
"tmp2.txt",
];
var_dump(sort($arr));
var_dump($arr);
var_dump(natsort($arr));
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:11:boolean true
D:\php20190701\php_code\0809\array_fun3.php:12:
array (size=5)
0 => string 'tmp1.txt' (length=8)
1 => string 'tmp10.txt' (length=9)
2 => string 'tmp15.txt' (length=9)
3 => string 'tmp2.txt' (length=8)
4 => string 'tmp22.txt' (length=9)
D:\php20190701\php_code\0809\array_fun3.php:13:boolean true
D:\php20190701\php_code\0809\array_fun3.php:14:
array (size=5)
0 => string 'tmp1.txt' (length=8)
3 => string 'tmp2.txt' (length=8)
1 => string 'tmp10.txt' (length=9)
2 => string 'tmp15.txt' (length=9)
4 => string 'tmp22.txt' (length=9)
複製代碼
natcasesort($array)
:用天然排序算法對數組進行不區分大小寫字母的排序<?php
$arr = [
"tmp15.txt",
"TMP10.txt",
"tmp1.txt",
"TMP22.txt",
"tmp2.txt",
];
var_dump(natsort($arr));
var_dump($arr);
var_dump(natcasesort($arr));
var_dump($arr);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:11:boolean true
D:\php20190701\php_code\0809\array_fun3.php:12:
array (size=5)
1 => string 'TMP10.txt' (length=9)
3 => string 'TMP22.txt' (length=9)
2 => string 'tmp1.txt' (length=8)
4 => string 'tmp2.txt' (length=8)
0 => string 'tmp15.txt' (length=9)
D:\php20190701\php_code\0809\array_fun3.php:13:boolean true
D:\php20190701\php_code\0809\array_fun3.php:14:
array (size=5)
2 => string 'tmp1.txt' (length=8)
4 => string 'tmp2.txt' (length=8)
1 => string 'TMP10.txt' (length=9)
0 => string 'tmp15.txt' (length=9)
3 => string 'TMP22.txt' (length=9)
複製代碼
array_multisort($arr[,$arg=SORT_STRING...])
:對多個數組或多維數組進行排序$array[] = array("age"=>20,"name"=>"li");
$array[] = array("age"=>21,"name"=>"ai");
$array[] = array("age"=>20,"name"=>"ci");
$array[] = array("age"=>22,"name"=>"di");
foreach ($array as $key=>$value){
$age[$key] = $value['age'];
$name[$key] = $value['name'];
}
array_multisort($age,SORT_NUMERIC,SORT_DESC,$name,SORT_STRING,SORT_ASC,$array);
var_dump($array);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:27:
array (size=4)
0 =>
array (size=2)
'age' => int 22
'name' => string 'di' (length=2)
1 =>
array (size=2)
'age' => int 21
'name' => string 'ai' (length=2)
2 =>
array (size=2)
'age' => int 20
'name' => string 'ci' (length=2)
3 =>
array (size=2)
'age' => int 20
'name' => string 'li' (length=2)
複製代碼
array_diff($array1,$array2[...])
:計算數組的差集array1
中, 可是不在array2
中, 多個同理...<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2);
var_dump($result);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:6:
array (size=1)
'd' => string 'yellow' (length=6)
複製代碼
if( !array_diff($arr1, $arr2) && !array_diff($arr2, $arr1)){
// 即相互都不存在差集,那麼這兩個數組就是相同的了,多數組也同樣的道理
echo '兩個數組相同';
}
複製代碼
array_diff_assoc($array1,$array2[,...])
:帶索引檢查計算數組的差集<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
var_dump($result);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:5:
array (size=3)
'b' => string 'brown' (length=5)
'c' => string 'blue' (length=4)
0 => string 'red' (length=3)
複製代碼
array_intersect($array1,$array2[...])
:計算數組的交集<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
var_dump($result);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:5:
array (size=2)
'a' => string 'green' (length=5)
0 => string 'red' (length=3)
複製代碼
array_intersect_assoc($array1,$array2[...])
:帶索引檢查計算數組的交集<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_intersect_assoc($array1, $array2);
var_dump($result);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:5:
array (size=1)
'a' => string 'green' (length=5)
複製代碼
array_slice($array,$offset[$length=null[,$preserve_keys=false]])
:截取數組<?php
$arr = [
'one'=>'a',
'two'=>'b',
'three'=>'c',
'four'=>'d',
'five'=>'e'
];
var_dump(array_slice($arr,2));
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:9:
array (size=3)
'three' => string 'c' (length=1)
'four' => string 'd' (length=1)
'five' => string 'e' (length=1)
複製代碼
array_splice($array,$offset[,$length=0,$replacement]])
:將數組中一部分去掉並用其它值替代<?php
$arr1=[
"a"=>"red",
"b"=>"green",
"c"=>"blue",
"d"=>"yellow"
];
$arr2=[
"a"=>"purple",
"b"=>"orange"
];
array_splice($arr1,0,2,$arr2);
var_dump($arr1);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:13:
array (size=4)
0 => string 'purple' (length=6)
1 => string 'orange' (length=6)
'c' => string 'blue' (length=4)
'd' => string 'yellow' (length=6)
複製代碼
array_merge($arr1[,$arr2...])
:合併數組<?php
$arr1 = ['first'=>1,'second'=>2,'third'=>3];
$arr2 = ['first'=>4,'second'=>5,'third'=>6];
$arr3 = [3=>'hello',4=>'world'];
var_dump(array_merge($arr1,$arr2)); // 後面的會覆蓋前面的
var_dump($arr1+$arr2); // 前面的會覆蓋後面的
var_dump(array_merge($arr3)); // 若是隻有一個數組, 而且是數字下標, 下標會從新索引
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:8:
array (size=3)
'first' => int 4
'second' => int 5
'third' => int 6
D:\php20190701\php_code\0809\array_fun3.php:9:
array (size=3)
'first' => int 1
'second' => int 2
'third' => int 3
D:\php20190701\php_code\0809\array_fun3.php:10:
array (size=2)
0 => string 'hello' (length=5)
1 => string 'world' (length=5)
複製代碼
array_chunk($array,$size[,$preserve_keys=false])
:將一個數組分割成多個<?php
$arr = array('a', 'b', 'c', 'd', 'e');
var_dump(array_chunk($arr, 2));
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:3:
array (size=3)
0 =>
array (size=2)
0 => string 'a' (length=1)
1 => string 'b' (length=1)
1 =>
array (size=2)
0 => string 'c' (length=1)
1 => string 'd' (length=1)
2 =>
array (size=1)
0 => string 'e' (length=1)
複製代碼
<?php
$arr = [
'one'=>'a',
'two'=>'b',
'three'=>'c',
];
var_dump(array_chunk($arr, 2, true));
var_dump(array_chunk($arr, 2, false));
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:7:
array (size=2)
0 =>
array (size=2)
'one' => string 'a' (length=1)
'two' => string 'b' (length=1)
1 =>
array (size=1)
'three' => string 'c' (length=1)
D:\php20190701\php_code\0809\array_fun3.php:8:
array (size=2)
0 =>
array (size=2)
0 => string 'a' (length=1)
1 => string 'b' (length=1)
1 =>
array (size=1)
0 => string 'c' (length=1)
複製代碼
array_column($array,$column_key[,$index_key])
:返回數組中指定的一列, 從多維數組中返回單列數組<?php
$records = [
[
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
], [
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
], [
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
], [
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
]
];
$first_names = array_column($records, 'first_name');
var_dump($first_names);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:23:
array (size=4)
0 => string 'John' (length=4)
1 => string 'Sally' (length=5)
2 => string 'Jane' (length=4)
3 => string 'Peter' (length=5)
複製代碼
<?php
$records = [
[
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
], [
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
], [
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
], [
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
]
];
$first_names = array_column($records, 'first_name','id');
var_dump($first_names);
複製代碼
D:\php20190701\php_code\0809\array_fun3.php:23:
array (size=4)
2135 => string 'John' (length=4)
3245 => string 'Sally' (length=5)
5342 => string 'Jane' (length=4)
5623 => string 'Peter' (length=5)
複製代碼