1、 PHP對數組的過濾php
函數: array_filter(p1[,p2])數組
參數p1是要過濾的數組,參數p2是自定義過濾會掉函數(能夠是匿名函數)函數
例子:this
1 <?php 2 $arr = ['',null,0,1,2]; 3 $newArr = array_filter($arr); 4 var_dump($newArr); 5 //結果是: [1,2] 6 //array_filter()在不填第二參數的狀況下會去除數組中爲false的元素
總結: array_filter(p1[,p2])函數能夠自定義過濾, 在不填寫第二參數的狀況下默認取出數組中爲false的元素;spa
2、 PHP取出數組中指定列.net
函數: array_column( array $input
, mixed $column_key
[, mixed $index_key
= null ])code
參數1是數組,參數2是要取出列的下標(做爲新數組的值),參數三是要取出列的下標(做爲新數組的鍵)對象
例子:blog
1 <?php 2 $arr = [ 3 ['id' => 1, 'name' => 'lili', 'sex' => 1],
4 ['id' => 2, 'name' => 'xiaoming', 'sex' => 0],
5 ]; 6 $columnArr = array_column($arr, 'id'); 7 var_dump($columnArr); 8 //結果: [1, 2]
9 $columnArr1 = array_column($arr, 'id', 'name'); 10 var_dump($columnArr1); 11 //結果: ['lili' => 1, 'xiaoming' => 2]
3、PHP對數組的去重
函數: array array_unique ( array [, int = SORT_STRING ] )$array$sort_flags
參數1是數組,參數2是數組值的排序方式,多個值重複,此函數去保留第一個遇到的值的下標.排序
例子:
1 <?php 2 $arr = [1,1,1,2,3,4]; 3 $arr1 = ['a' => 1, 'b' => 1, 'c' => 3]; 4 var_dump(array_unique($arr)); //array(4) { [0]=> int(1) [3]=> int(2) [4]=> int(3) [5]=> int(4) }
5 var_dump(array_unique($arr1));//array(2) { ["a"]=> int(1) ["c"]=> int(3) }
4、PHP取出兩個數組中不一樣的元素
函數: array array_diff ( array $array1
, array $array2
[, array $...
] )
此函數能夠有多個參數,此函數拿參數1和其餘參數比較,取只在參數1中有的元素
例子:
1 <?php 2 $arr = [1, 2, 3]; 3 $arr1 = [4, 5, 1]; 4 $arr2 = [6, 7, 3]; 5
6 var_dump(array_diff($arr, $arr1, $arr2)); //array(1) { [1]=> int(2) }
5、 PHP取出兩個數組的交集
函數: array array_intersect ( array $array1
, array $array2
[, array $...
] )
此函數和array_diff正好相反, 取出多個數組中都存在的元素, 也能夠說是交集.
例子:
1 <?php 2 $arr = [1, 2, 3]; 3 $arr1 = [3, 5, 1]; 4 $arr2 = [1, 7, 3]; 5
6 var_dump(array_intersect($arr, $arr1, $arr2)); 7 //array(2) { [0]=> int(1) [2]=> int(3) }
6、 PHP對數組的遍歷遞歸
函數: array_walk_recursive()
對數組中的每一個成員遞歸地應用用戶函數
1 <?php 2 $arr = [['id' => 1, 'name' => 'lii'], ['id' => 2, 'name' => 'haa']]; 3
4 $tmpArr1 = []; 5 $tmpArr2 = []; 6 array_walk_recursive($arr, function($v, $k)use(&$tmpArr1, &$tmpArr2){ 7 array_push($tmpArr1, $k); 8 array_push($tmpArr2, $v); 9 }); 10 var_dump($tmpArr1); 11 //array(4) { [0]=> string(2) "id" [1]=> string(4) "name" [2]=> string(2) "id" [3]=> string(4) "name" }
12 echo '<hr />'; 13 var_dump($tmpArr2); 14 //array(4) { [0]=> int(1) [1]=> string(3) "lii" [2]=> int(2) [3]=> string(3) "haa" }
7、 PHP對數組的遍歷迭代
函數: array_reduce()
對數組的元素進行遍歷迭代
例子:
1 $arr = [['id' => 1, 'name' => 'lii'], ['id' => 2, 'name' => 'haa']]; 2 $result = array_reduce($arr, function($tmp, $v){ 3 array_push($tmp, $v); 4
5 return $tmp; 6 }, []); 7
8 var_dump($result); 9 //array(2) { [0]=> array(2) { ["id"]=> int(1) ["name"]=> string(3) "lii" } [1]=> array(2) { ["id"]=> int(2) ["name"]=> string(3) "haa" } }
第一次遍歷把['id' => 1, 'name' => 'lii'] 放進$tmp(空數組)中
第二次遍歷把['id' => 2, 'name' => 'haa'] 放進$tmp中
結果:
array(2) { [0]=> array(2) { ["id"]=> int(1) ["name"]=> string(3) "lii" } [1]=> array(2) { ["id"]=> int(2) ["name"]=> string(3) "haa" } }
8、 PHP預約義接口ReflectionClass
映射類接口, 能夠反射一個類的映射信息
例子:
1 class Person 2 { 3 public $id; 4 public function __construct($id) 5 { 6 $this->id = $id; 7 } 8 } 9 $reflection = new ReflectionClass('Person'); 10 $object = $reflection->newInstanceArgs(['id' => 1]); 11 var_dump($object); //object(Person)#113 (1) { ["id"]=> int(1) }
9、 PHP 預約義接口Closure
1. bindTo //給匿名函數綁定指定的$this對象和類做用域.
2. bind //給匿名函數綁定指定的$this對象和類做用域。,
例子:
1 class A { 2 function __construct($val) { 3 $this->val = $val; 4 } 5 public function getClosure() 6 { 7 //returns closure bound to this object and scope
8 return function() 9 { 10 return $this->val; 11 }; 12 } 13 } 14
15 $ob1 = new A(1); 16 $ob2 = new A(2); 17
18 $cl = $ob1->getClosure(); 19 echo $cl(), "\n"; //1
20 $cl = $cl->bindTo($ob2); 21 echo $cl(), "\n"; //2
22 var_dump($cl instanceof \Closure); //bool(true)
1 class A { 2 private static $sfoo = 1; 3 private $ifoo = 2; 4 } 5 $cl1 = static function() { 6 return A::$sfoo; 7 }; 8 $cl2 = function() { 9 return $this->ifoo; 10 }; 11
12 $bcl1 = Closure::bind($cl1, null, 'A'); 13 $bcl2 = Closure::bind($cl2, new A(), 'A'); 14 echo $bcl1(), "\n"; //1
15 echo $bcl2(), "\n"; //2