Home >Backend Development >PHP Problem >How to determine whether the specified key value exists in a two-dimensional array in PHP

How to determine whether the specified key value exists in a two-dimensional array in PHP

PHPz
PHPzOriginal
2023-04-20 13:54:231005browse

For PHP's two-dimensional array, we often need to determine whether a key-value pair exists in the array. This article will introduce several common methods.

  1. Use in_array function

The in_array function can be used to determine whether a value exists in an array, but it can only determine whether a value exists in a one-dimensional array and cannot determine whether it is in a two-dimensional array. key-value pairs. We can use its variant in_array_recursive function to recursively determine whether the key-value pair in the two-dimensional array exists.

Sample code:

function in_array_recursive($needle, $haystack, $strict = false)
{
    foreach ($haystack as $value) {
        if ((!$strict && $needle == $value) || ($strict && $needle === $value)) {
            return true;
        }
        if (is_array($value) && in_array_recursive($needle, $value, $strict)) {
            return true;
        }
    }
    return false;
}

$array = array(
    array('id' => 1, 'name' => '张三'),
    array('id' => 2, 'name' => '李四'),
    array('id' => 3, 'name' => '王五'),
);

if (in_array_recursive(array('id' => 1, 'name' => '张三'), $array)) {
    echo '存在';
} else {
    echo '不存在';
}
  1. Use array_column function

array_column function can extract a column in a two-dimensional array as a new one-dimensional array, We can use it to determine whether a key exists in the array.

Sample code:

$array = array(
    array('id' => 1, 'name' => '张三'),
    array('id' => 2, 'name' => '李四'),
    array('id' => 3, 'name' => '王五'),
);

$ids = array_column($array, 'id');

if (in_array(1, $ids)) {
    echo '存在';
} else {
    echo '不存在';
}
  1. Use array_search function

array_search function can find the location of a key value in the array, and if found, return the The index of the key, otherwise false is returned. However, it can only find one-dimensional arrays and cannot find key-value pairs in two-dimensional arrays. We can use its third parameter to specify the value of the lookup key.

Sample code:

$array = array(
    array('id' => 1, 'name' => '张三'),
    array('id' => 2, 'name' => '李四'),
    array('id' => 3, 'name' => '王五'),
);

foreach ($array as $key => $value) {
    if (array_search(1, $value)) {
        echo '存在';
        break;
    } else {
        echo '不存在';
    }
}

The above are three common two-dimensional array judgment methods. You can choose the appropriate method according to the specific situation.

The above is the detailed content of How to determine whether the specified key value exists 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