[PHP daily function] cycle 03

Keywords: PHP

2019-02-27

array_keys()

array_keys ( array $array [, mixed $search_value = null [, bool $strict = false ]] ) : array
Description: get some or all key names in the array

$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
Description: applies a callback function to each element of the 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
Description: merge one or more arrays

$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
Note: sort multiple arrays or multidimensional arrays (see the official website for details)

$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
Note: the last cell in the pop-up array

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

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

array_product()

array_product ( array $array ) : number
Description: calculate the product of all values in the array

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

// output
12

Before, because of some things, I didn't change every day. Later, I will keep changing every day

Posted by vbmurray on Sun, 08 Dec 2019 01:00:22 -0800