Home >Backend Development >PHP Tutorial >PHP array function application case

PHP array function application case

WBOY
WBOYOriginal
2023-06-20 09:48:421608browse

PHP is a commonly used Web programming language, and arrays are one of the most important data types. In PHP, there are many powerful array functions for processing array data, which can greatly improve development efficiency. The following will introduce some commonly used PHP array functions and give some practical application cases.

  1. array_sort

The array_sort function is used to sort arrays. There are multiple sorting methods available. Here is an example:

function array_sort($arr, $sort_key, $sort_order = SORT_ASC) {
    if (empty($arr) || !is_array($arr)) {
        return $arr;
    }
    $keys = array_keys($arr);
    if (!is_numeric($sort_key)) {
        $sort_key_arr = array();
        foreach ($keys as $k) {
            $sort_key_arr[$k] = $arr[$k][$sort_key];
        }
        $sort_key = $sort_key_arr;
    }
    array_multisort($sort_key, $sort_order, $keys);
    $sorted_arr = array();
    foreach ($keys as $k) {
        $sorted_arr[$k] = $arr[$k];
    }
    return $sorted_arr;
}

The arguments to this function are an array, a key to sort by, and a sort order (default is ascending).

Application case: Sort a multi-dimensional array according to a certain key value.

$students = array(
    array('name' => 'Mike', 'score' => 80),
    array('name' => 'John', 'score' => 90),
    array('name' => 'Alice', 'score' => 70)
);
$students_sorted = array_sort($students, 'score', SORT_DESC);
print_r($students_sorted);

Output result:

Array
(
    [1] => Array
        (
            [name] => John
            [score] => 90
        )

    [0] => Array
        (
            [name] => Mike
            [score] => 80
        )

    [2] => Array
        (
            [name] => Alice
            [score] => 70
        )

)
  1. array_filter

array_filter function is used to filter elements in the array according to specified conditions, and you can customize the filtering rules. The following is an example:

function array_filter_custom($arr, $func) {
    if (empty($arr) || !is_array($arr)) {
        return $arr;
    }
    $result = array();
    foreach ($arr as $key => $value) {
        if ($func($value, $key)) {
            $result[$key] = $value;
        }
    }
    return $result;
}

The parameters of this function are an array and a custom callback function. The callback function returns a bool value, which is used to determine whether the current element needs to be retained.

Application case: filter even numbers in an array.

$numbers = array(1, 2, 3, 4, 5, 6, 7, 8);
$even_numbers = array_filter_custom($numbers, function($value) {
    return $value % 2 == 0;
});
print_r($even_numbers);

Output result:

Array
(
    [1] => 2
    [3] => 4
    [5] => 6
    [7] => 8
)
  1. array_map

array_map function is used to apply a callback function to each element in the array and return a new array. The following is an example:

function array_map_custom($arr, $func) {
    if (empty($arr) || !is_array($arr)) {
        return $arr;
    }
    $result = array();
    foreach ($arr as $key => $value) {
        $result[$key] = $func($value);
    }
    return $result;
}

The parameters of this function are an array and a custom callback function. The callback function should accept one parameter and return the converted value.

Application case: Convert strings in an array to uppercase.

$strings = array('hello', 'world', 'php');
$strings_upper = array_map_custom($strings, function($value) {
    return strtoupper($value);
});
print_r($strings_upper);

Output result:

Array
(
    [0] => HELLO
    [1] => WORLD
    [2] => PHP
)
  1. array_reduce

array_reduce function is used to apply a callback function to each element in the array one by one, and finally returns A reduced value. Here is an example:

function array_reduce_custom($arr, $func, $initial = null) {
    if (empty($arr) || !is_array($arr)) {
        return $initial;
    }
    $result = $initial;
    foreach ($arr as $value) {
        $result = $func($result, $value);
    }
    return $result;
}

The parameters of this function are an array, a custom callback function, and an optional initial value.

Application case: Add all the values ​​in an array.

$numbers = array(1, 2, 3, 4, 5);
$sum = array_reduce_custom($numbers, function($carry, $item) {
    return $carry + $item;
});
echo $sum;

Output results:

15

Through these practical application cases, you can better understand the use of PHP array functions, thereby processing array data more conveniently during the programming process.

The above is the detailed content of PHP array function application case. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn