array_change_key_case(array $arr, [, int $case = CASE_LOWER]) : array
php
說明:將全部鍵名改成全大寫或者小寫數組
$arr = [ 'name' => '張三', 'Age' => 18, 'gender' => '男', ]; // case upper print_r(array_change_key_case($arr, CASE_UPPER)); // output Array ( [NAME] => 張三 [AGE] => 18 [GENDER] => 男 )
array_chunk ( array $array , int $size [, bool $preserve_keys = false ] ) : array
說明:將一個數組分割成多個數組函數
$arr = [ 'name' => '張三', 'Age' => 18, 'gender' => '男', ]; print_r(array_chunk($arr, 2)); // output Array ( [0] => Array ( [0] => 張三 [1] => 18 ) [1] => Array ( [0] => 男 ) )
array_column ( array $input , mixed $column_key [, mixed $index_key = null ] ) : array
說明:返回數組中指定的一列code
$arr = [ 0 => [ 'name' => 'LeiJian', 'age' => 18, 'gender' => '男', ], 1 => [ 'name' => 'ZhangSan', 'age' => 19, 'gender' => '男', ], 2 => [ 'name' => 'LiSi', 'age' => 20, 'gender' => '女', ], ]; print_r(array_column($arr, NULL, 'name')); // output Array ( [LeiJian] => Array ( [name] => LeiJian [age] => 18 [gender] => 男 ) [ZhangSan] => Array ( [name] => ZhangSan [age] => 19 [gender] => 男 ) [LiSi] => Array ( [name] => LiSi [age] => 20 [gender] => 女 ) )
array_combine ( array $keys , array $values ) : array
說明:將一個數組做鍵值,另外一個數組做值產生一個新的數組索引
$a = [1, 2, 3]; $b = ['a', 'b', 'c']; print_r(array_combine($a, $b)); // output Array ( [1] => a [2] => b [3] => c )
array_count_values ( array $array ) : array
說明:數組中的鍵是 array 中的值,數組中的值是 array 的值出現的次數input
$arr = [1, 'hello', 1, 'world', 'world']; print_r(array_count_values($arr)); // output Array ( [1] => 2 [hello] => 1 [world] => 2 )
array_diff_key ( array $array1 , array $array2 [, array $... ] ) : array
使用鍵名(key)比較數組的差集回調函數
$arr1 = ['first' => 2, 'third' => 3, 'fourth' => 4]; $arr2 = ['first' => 1, 'second' => 2, 'third' => 3]; print_r(array_diff_key($arr1, $arr2)); // output Array ( [fourth] => 4 )
array_diff_uassoc ( array $array1 , array $array2 [, array $... ], callable $key_compare_func ) : array
說明:使用回調函數作索引檢查數組差集io
$arr1 = ['first' => 2, 'third' => 3, 'fourth' => 4]; $arr2 = ['first' => 1, 'second' => 2, 'third' => 3]; print_r(array_diff_uassoc($arr1, $arr2, function($a, $b){ if ($a === $b) { return 0; } return ($a > $b) ? 1 : -1; })); // output Array ( [first] => 2 [fourth] => 4 )
`array_diff ( array $array1 , array $array2 [, array $... ] ) : array
`
說明:比較數組間的差集(array1 比較數組,array2 被比較)function
$arr1 = [1, 2, 3, 4, 6]; $arr2 = [2, 3, 4, 5]; print_r(array_diff($arr1, $arr2)); // output Array ( [0] => 1 [4] => 6 )
array_fill_keys ( array $keys , mixed $value ) : array
說明:使用指定鍵值(數組)和值進行填充數組class
$arr1 = [1, 2, 3, 4, 6]; print_r(array_fill_keys($arr1, 'HaHaHa~')); // output Array ( [1] => HaHaHa~ [2] => HaHaHa~ [3] => HaHaHa~ [4] => HaHaHa~ [6] => HaHaHa~ )
`array_fill ( int $start_index , int $num , mixed $value ) : array
`
說明:用給定的值進行填充數組,start_index
索引開始位置,若爲負值,則從 0 開始,num
爲填充數量
print_r(array_fill(2, 3, 'HaHaHa~')); // output Array ( [2] => HaHaHa~ [3] => HaHaHa~ [4] => HaHaHa~ )
array_filter ( array $array [, callable $callback [, int $flag = 0 ]] ) : array
說明:用回調函數過濾數組中的值(flag
決定 callback
接收的參數)
$arr1 = [1, 2, 3, 4, 6]; print_r(array_filter($arr1, function($value){ if ($value > 2) { return $value; } })); // output Array ( [2] => 3 [3] => 4 [4] => 6 )