Check if value exists in multidimensional array us

2020-02-20 07:42发布

I need to check if a value exists in a multidimensional array. I found this example on Stackoverflow and on PHP.NET which I like because its an elegant and compact solution, but I noticed a weird behavior:

$userdb=Array
(
(0) => Array
    (
        (uid) => '100',
        (name) => 'Sandra Shush',
        (pic_square) => 'urlof100'
    ),

(1) => Array
    (
        (uid) => '5465',
        (name) => 'Stefanie Mcmohn',
        (pic_square) => 'urlof100'
    ),

(2) => Array
    (
        (uid) => '40489',
        (name) => 'Michael',
        (pic_square) => 'urlof40489'
    )
);



if(array_search(100, array_column($userdb, 'uid'))) {
    echo "FOUND";
}

The IF statement does not return any value if you check the existence of any value of the FIRST array (Array [0]). It does work with the values of the other arrays. Try with 100 first, then try with 40489 (or try with a "name" or "pic_square").

4条回答
Evening l夕情丶
2楼-- · 2020-02-20 07:51

That's because array_search returns the key, which in this case is 0, which will equate to false. You need to do a strict comparison, ie:

if (false !== array_search(100, array_column($userdb, 'uid'))) {
    ...  
}

See here

查看更多
别忘想泡老子
3楼-- · 2020-02-20 08:02

It is because, your value 100 is in first index 0, so if condition fails. Instead compare with false strictly:

DEMO

if(array_search(100, array_column($userdb, 'uid')) !== False) {
    echo "FOUND";
} else {
    echo "Not Found";
}

For this instance, it is better to use in_array, since you are just checking like if the element is in array or not. Use, array_search if you want to retrieve the index of the element.

查看更多
三岁会撩人
4楼-- · 2020-02-20 08:05

You can use in_array() like below:-

<?php
$userdb=Array
(
'0' => Array
    (
        'uid' => '100',
        'name' => 'Sandra Shush',
        'url' => 'urlof100'
    ),

'1' => Array
    (
        'uid' => '5465',
        'name' => 'Stefanie Mcmohn',
        'pic_square' => 'urlof100'
    ),

'2' => Array
    (
        'uid' => '40489',
        'name' => 'Michael',
        'pic_square' => 'urlof40489'
    )
);
print_r(array_column($userdb, 'uid')); 
if(in_array(100, array_column($userdb, 'uid'))) { // search value in the array
    echo "FOUND";
}
?>

Output:- https://eval.in/1058147

Note:- you code will work also if you modify your comparison like below:-

if(array_search(100, array_column($userdb, 'uid')) !== false) {
    echo "FOUND";
} else {
    echo "Not Found";
}

through the manual:- http://php.net/manual/en/function.array-search.php

It sates:-

Returns the key for needle if it is found in the array, FALSE otherwise.

php treated 0 as false and 1 as true. That's why your code fails, because your code returns 0 (as match found on the very first index of array).

查看更多
▲ chillily
5楼-- · 2020-02-20 08:06

You May Try This:

$res1 =Array
(
'0' => Array
    (
        'uid' => '100',
        'name' => 'Sandra Shush',
        'url' => 'urlof100'
    ),

'1' => Array
    (
        'uid' => '5465',
        'name' => 'Stefanie Mcmohn',
        'pic_square' => 'urlof100'
    )
);

$res2 =Array
(
'0' => Array
    (
        'uid' => '40489',
        'name' => 'Michael',
        'pic_square' => 'urlof40489'
    )

'1' => Array
    (
        'uid' => '5465',
        'name' => 'Stefanie Mcmohn',
        'pic_square' => 'urlof100'
    )
);

foreach ($res1 as $key=>$item1)
{
    $val=$item1['uid'];
    foreach ($res2 as $key2=>$item2)
    {
          if ($item2['uid'] == $val)
           {
                //value 5465 found
           }
    }
}
查看更多
登录 后发表回答