Home  >  Article  >  Backend Development  >  How to determine whether a value is in a two-dimensional array in php

How to determine whether a value is in a two-dimensional array in php

PHPz
PHPzOriginal
2023-04-18 09:05:36540browse

With the continuous development of the Internet, website development has become more and more common. PHP is a very popular development language, and many websites are developed using PHP. In PHP development, two-dimensional arrays are often used, which can easily store and search a certain amount of data. This article will introduce how to determine whether a value is in a two-dimensional array in PHP.

First of all, we need to understand the concept of two-dimensional array. Simply put, a two-dimensional array is an array in which each element is also an array. Each subarray has a corresponding key value, and these subarrays can be accessed through the keys of the two-dimensional array. For example, the following is a simple two-dimensional array:

$people = array(
    array(
        "name" => "John",
        "age" => 25,
        "gender" => "male"
    ),
    array(
        "name" => "Jane",
        "age" => 30,
        "gender" => "female"
    ),
    array(
        "name" => "Bob",
        "age" => 20,
        "gender" => "male"
    )
);

The above two-dimensional array represents the information of three people, and each person is stored in a sub-array. Each subarray has three key values: name represents the name, age represents the age, and gender represents the gender. Now, we want to determine whether a person is in this array, what should we do?

A simple way is to use a foreach loop to iterate through this array. For each subarray, we can check one by one whether its key value matches the value we are looking for. The code looks like this:

function in_array_2d($needle, $haystack) {
    foreach ($haystack as $item) {
        foreach ($item as $key => $value) {
            if ($value == $needle) {
                return true;
            }
        }
    }
    return false;
}

$person = array(
    "name" => "Jane",
    "age" => 30,
    "gender" => "female"
);

if (in_array_2d("male", $people)) {
    echo "The array contains a male person.";
} else {
    echo "The array does not contain a male person.";
}

if (in_array_2d($person, $people)) {
    echo "The array contains the person Jane.";
} else {
    echo "The array does not contain the person Jane.";
}

The above code defines a in_array_2d function that accepts two parameters: the value to find and the two-dimensional array to search. There are two foreach loops nested in the function, which are used to traverse the key values ​​​​in all sub-arrays. If a matching value is found, the function returns true, otherwise it returns false. Note that in the second example, we used an associative array to represent the information about the person we are looking for.

Although the above implementation is very simple, it is not very efficient. If the two-dimensional array you are looking for is large, traversing the entire array can be very time-consuming. Therefore, we need a more efficient method.

A better way is to use PHP's array_column function. This function can return the specified element or key-value pair in an array. For example, if we want to get the names of all people from the 2D array above, we can use the following code:

$names = array_column($people, "name");
print_r($names);

This will output the following:

Array
(
    [0] => John
    [1] => Jane
    [2] => Bob
)

Now, we just need to By searching for the value you are looking for in the returned array, you can determine whether it is in a two-dimensional array. The following is the implementation code:

function in_array_2d($needle, $haystack) {
    $values = array_column($haystack, null, 0);
    return in_array($needle, $values);
}

$person = array(
    "name" => "Jane",
    "age" => 30,
    "gender" => "female"
);

if (in_array_2d("male", $people)) {
    echo "The array contains a male person.";
} else {
    echo "The array does not contain a male person.";
}

if (in_array_2d($person, $people)) {
    echo "The array contains the person Jane.";
} else {
    echo "The array does not contain the person Jane.";
}

The above code implements the in_array_2d function, but it is a little more complicated. First, we use the array_column function to get the first element of all subarrays in the two-dimensional array to be searched and use it as the key of the new array. In this way, we can use the in_array function to check whether the value we are looking for is in the new array. Note that in the second example, we used an associative array to represent the information about the person we are looking for.

The advantage of using the array_column function is that we can get specific values ​​in the subarray by specifying the key. This means we can easily find a subarray containing a specific value from a 2D array. Here is an example:

$male = array_column($people, null, "gender")["male"];
print_r($male);

This will output a subarray containing male information.

Although the above methods can be used to determine whether a value is in a two-dimensional array, their efficiency and readability are different. foreachLoops are probably the easiest to understand, but they are less efficient. The array_column function is more efficient for large two-dimensional arrays, but it requires more code to implement. Therefore, when choosing a method, the decision on which method is more suitable should be made on a case-by-case basis.

The above is the detailed content of How to determine whether a value is in a two-dimensional array in php. 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