简体   繁体   中英

Check if particular value exists in multidimensional array

I have a multidimensional array like this

Array
(
    [jack] => Array
        (
            [0] => 12
            [1] => 45
            [2] => 78
            [3] => 19
            [4] => 94
            [5] => 668
        )

    [john] => Array
        (
            [0] => 641
            [1] => 741
            [2] => 683
            [3] => 603
        )

)

How can i search for 641 and if found return it's parent key. ie. i want to get john if 641 is found.

This is what i have tried

    if(in_array_r("641" , $array)){
    // if found return john!
}

OR

if(in_array_r("12" , $array)){
    // if found return jack!
}

I'm looking for a way that won't include a foreach loop . A solution with less complexity would be nice.

Try this:

$arr = [
    'jack' => [
        12,
        45,
        78,
        19,
        94,
        668,
    ],
    'john' => [
        641,
        741,
        683,
        603,
    ],
];

function in_array_r ($needle, $haystack) {
    foreach ($haystack as $key => $subArr) {
        if (in_array($needle, $subArr)) {
            return $key;
        }
    }
    return false;
}

echo in_array_r(641, $arr); // => john
echo "\n";
echo in_array_r(12, $arr); // => jack

eval.in demo

The code is just running in_array in a foreach. This'll work on 2D arrays, but not on arrays with more dimensions. Let me know if your question is actually about more than 2D arrays.

Best Way To Find is As Follow

<?php

$myarr = Array
(
'Jack' => Array
    (
        'uid' => '100',
        'name' => 'Sandra Shush',
        'url' => 'urlof100'
    ),

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

echo checkData($myarr,100);

 function checkData( $myarr,$myvalue ) 
 {
        foreach( $myarr as $key => $value ){
            if( in_array(  $myvalue, $value ) ){
                return $key;
            }
        }
        return false;
 }

Check It http://phptester.net/

I'm assuming you made use of a recursive in_array function as from here: https://gist.github.com/Billy-/bc6865066981e80e097f

I have modified it to suit your requirement here (so it returns key of array now as you desired):

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict)!==FALSE)) {
            return key($haystack);
        }
    }

    return false;
}

You should be able to use this as:

echo in_array_r('641',$array); // Prints john
echo in_array_r('12',$array); // Prints jack

Where the array I'm assuming is as of your question:

Array
(
    [jack] => Array
        (
            [0] => 12
            [1] => 45
            [2] => 78
            [3] => 19
            [4] => 94
            [5] => 668
        )

    [john] => Array
        (
            [0] => 641
            [1] => 741
            [2] => 683
            [3] => 603
        )

)
$arr = Array(
    "jack" => Array(12, 45, 78, 19, 94, 668),
    "john" => Array("641", 741, 683, 603),
);
$tofind = 641;

function getKey($arr, $tofind) {
    foreach($arr as $key => $val){
        if(in_array($tofind, $val)){
            return $key;
        }
    }
    return "not found";
}

echo getKey($arr, $tofind);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM