【PHP 每日函数】第 02 周期

2019 - 02 - 22

array_flip()

array_flip ( array $array ) : array
说明:交换数组中的键和值

$arr = [
    'name' => 'ZhangSan',
    'age' => 18,
    'gender' => 'man'
];

print_r(array_flip($arr));

// output
Array
(
    [ZhangSan] => name
    [18] => age
    [man] => gender
)

array_intersect_assoc()

array_intersect_assoc ( array $array1 , array $array2 [, array $... ] ) : array
说明:带索引检查数组的交集

$arr1 = [
    'name' => 'ZhangSan',
    'age' => 18,
    'gender' => 'man'
];


$arr2 = [
    'name' => 'ZhangSan',
    'age1' => 18,
    'gender' => 'woman',
];
print_r(array_intersect_assoc($arr1, $arr2));

// output
Array
(
    [name] => ZhangSan
)

array_intersect_key()

`array_intersect_key ( array $array1 , array $array2 [, array $... ] ) : array
`
说明:使用键名计算数组的交集

$arr1 = [
    'name' => 'ZhangSan',
    'age' => 18,
    'gender' => 'man'
];


$arr2 = [
    'name' => 'ZhangSan',
    'age1' => 18,
    'gender' => 'woman',
];
print_r(array_intersect_key($arr1, $arr2));

// output
Array
(
    [name] => ZhangSan
    [gender] => man
)

array_intersect()

array_intersect ( array $array1 , array $array2 [, array $... ] ) : array
说明:计算数组的交集

$arr1 = [
    'name1' => 'ZhangSan',
    'age' => 18,
    'gender' => 'man'
];


$arr2 = [
    'name' => 'ZhangSan',
    'age1' => 18,
    'gender' => 'woman',
];
print_r(array_intersect($arr1, $arr2));

// output
Array
(
    [name1] => ZhangSan
    [age] => 18
)

array_key_exists()

`array_key_exists ( mixed $key , array $array ) : bool
`
说明:检查数组中是否含有指定的键名/索引

$arr1 = [
    'name1' => 'ZhangSan',
    'age' => 18,
    'gender' => 'man'
];


print_r(array_key_exists('name1', $arr1));

// output
boolean(true);

php

相关推荐