【PHP 每日函數】第 03 週期

2019-02-27

array_keys()

array_keys ( array $array [, mixed $search_value = null [, bool $strict = false ]] ) : array
說明:獲取數組中部分的或者全部的鍵名php

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

print_r(array_keys($arr));

// output
Array
(
    [0] => name
    [1] => age
    [2] => gender
)

array_map()

array_map ( callable $callback , array $array1 [, array $... ] ) : array
說明:爲數組的每一個元素應用回調函數數組

$arr = [1, 2, 6];

print_r(array_map(function($value){
    return $value + 1;
}, $arr))

// output
Array
(
    [0] => 2
    [1] => 3
    [2] => 7
)

array_merge()

array_merge ( array $array1 [, array $... ] ) : array
說明:合併一個或者多個數組函數

$arr = [1, 2, 6];

$arr2 = [2, 6, 7];

print_r(array_merge($arr, $arr2));

// output
Array
(
    [0] => 1
    [1] => 2
    [2] => 6
    [3] => 2
    [4] => 6
    [5] => 7
)

array_multisort()

array_multisort ( array &$array1 [, mixed $array1_sort_order = SORT_ASC [, mixed $array1_sort_flags = SORT_REGULAR [, mixed $... ]]] ) : bool
說明:對多個數組或者多維數組進行排序(具體可查看官網)code

$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);

foreach ($data as $key => $value) {
    $volume[$key] = $value['volume'];
    $edition[$key] = $value['edition'];
}

array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);

print_r($data);

// output
Array
(
    [0] => Array
        (
            [volume] => 98
            [edition] => 2
        )

    [1] => Array
        (
            [volume] => 86
            [edition] => 1
        )

    [2] => Array
        (
            [volume] => 86
            [edition] => 6
        )

    [3] => Array
        (
            [volume] => 85
            [edition] => 6
        )

    [4] => Array
        (
            [volume] => 67
            [edition] => 2
        )

    [5] => Array
        (
            [volume] => 67
            [edition] => 7
        )

)

array_pop()

array_pop ( array &$array ) : mixed
說明:彈出數組中最後一個單元排序

$arr = [1, 2, 6];
array_pop($arr);
print_r($arr);

// output
Array
(
    [0] => 1
    [1] => 2
)

array_product()

array_product ( array $array ) : number
說明:計算數組中全部值的乘積回調函數

$arr = [1, 2, 6];
print_r(array_product($arr));

// output
12
以前由於一些事,就沒有天天都更,之後會堅持保持天天都更
相關文章
相關標籤/搜索