1

2019 - 02 - 20

array_change_key_case()

array_change_key_case(array $arr, [, int $case = CASE_LOWER]) : array

说明:将所有键名改为全大写或者小写

$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_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_column ( array $input , mixed $column_key [, mixed $index_key = null ] ) : array
说明:返回数组中指定的一列

$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_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_count_values ( array $array ) : array
说明:数组中的键是 array 中的值,数组中的值是 array 的值出现的次数

$arr = [1, 'hello', 1, 'world', 'world'];

print_r(array_count_values($arr));

// output
Array
(
    [1] => 2
    [hello] => 1
    [world] => 2
)

2019 - 02 - 21

array_diff_key()

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_diff_uassoc ( array $array1 , array $array2 [, array $... ], callable $key_compare_func ) : array
说明:使用回调函数做索引检查数组差集

$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_diff ( array $array1 , array $array2 [, array $... ] ) : array
`
说明:比较数组间的差集(array1 比较数组,array2 被比较)

$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_fill_keys ( array $keys , mixed $value ) : array
说明:使用指定键值(数组)和值进行填充数组

$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()

`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_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
)

SayHe110
14 声望5 粉丝

Web Developer