Home  >  Article  >  Backend Development  >  PHP multidimensional array query for a certain value

PHP multidimensional array query for a certain value

WBOY
WBOYOriginal
2023-05-05 21:21:06633browse

In PHP, an array is a very common data structure. It is a data type that can store multiple values. Multidimensional arrays refer to an array that can store other arrays, that is, arrays of arrays. Multidimensional arrays are very suitable for certain situations, but when querying specific values, you may need to use some special methods and techniques. This article will introduce how to query a certain value in a multidimensional array in PHP.

  1. Using a loop to traverse an array

In PHP, the simplest way to query a value in a multidimensional array is to use a loop to traverse the array. The specific implementation process is as follows:

function searchValue($arr, $value) {
    foreach ($arr as $key => $val) {
        if (is_array($val)) {
            $result = searchValue($val, $value);
            if ($result !== false) {
                return $result;
            }
        } else if ($val === $value) {
            return $key;
        }
    }
    return false;
}

The above code uses a recursive method to traverse the multi-dimensional array, going deeper layer by layer until the target value is found or the entire array is traversed. If the target value is found, returns the index at which the value is located; otherwise returns false.

  1. Using PHP array functions

In addition to using loop traversal, we can also use some of PHP's own array functions to query a certain value in a multi-dimensional array.

(1)array_walk_recursive function

The array_walk_recursive function can traverse all elements in a multi-dimensional array and call the callback function to process each element. The specific implementation process is as follows:

function searchValue($arr, $value) {
    $found = false;
    array_walk_recursive($arr, function($val, $key) use ($value, &$found) {
        if ($val === $value) {
            $found = $key;
        }
    });
    return $found;
}

The above code nests the callback function of the array_walk_recursive function in the searchValue function to find the target value. If found, the index of the location of the value is stored in the $found variable. , and returns the value of the variable.

(2)array_column function

The array_column function can obtain the value of the specified column from a multi-dimensional array. Therefore, if we want to query whether a certain value exists in a multidimensional array, we can first use the array_column function to get all the values ​​​​of the column where the value is located, and then query it. The specific implementation process is as follows:

function searchValue($arr, $value) {
    $col = array_column($arr, null);
    if (in_array($value, $col)) {
        return array_search($value, $col);
    } else {
        return false;
    }
}

The above code uses the array_column function to obtain the values ​​of all elements in the entire multi-dimensional array and store them in the $col variable. Then use the in_array function to determine whether the target value exists in the $col array. If found, use the array_search function to return the index where the value is located; otherwise, return false.

  1. Using third-party libraries

In addition to using the functions that come with PHP, we can also use functions in third-party libraries to process multi-dimensional arrays. Among them, jQuery's grep function can be used for array filtering and search. The usage method is as follows:

function searchValue($arr, $value) {
    $result = null;
    foreach ($arr as $key => $val) {
        if (is_array($val)) {
            $result = searchValue($val, $value);
        } else if ($val === $value) {
            $result = $key;
        }
        if ($result) {
            break;
        }
    }
    return $result;
}

The above code uses recursive method to traverse the multi-dimensional array and uses jQuery's grep function to find the target value. If the target value is found, returns the index at which the value is located; otherwise returns null.

Summary

This article introduces the method of querying a certain value in a multi-dimensional array in PHP, including using loops to traverse the array, PHP's own array functions, and functions in third-party libraries . These methods can be selected and used according to specific circumstances to achieve the most efficient query results.

The above is the detailed content of PHP multidimensional array query for a certain value. 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