user1114748 user1114748 - 3 months ago 5
PHP Question

Looking for a value in a multidimensional array in PHP

I have this kind of array

array(1) {
[36694730] => array(2) {
["summonerId"] => int(36694730)["pages"] => array(6) {
[0] => array(4) {
["id"] => int(35938350)["name"] => string(10)
"Fervor ADC" ["current"] => bool(true)["masteries"] => array(10) {
[0] => array(2) {
["id"] => int(6343)["rank"] => int(1)
}[1] => array(2) {
["id"] => int(6131)["rank"] => int(5)
}[2] => array(2) {
["id"] => int(6122)["rank"] => int(1)
}[3] => array(2) {
["id"] => int(6331)["rank"] => int(5)
}[4] => array(2) {
["id"] => int(6111)["rank"] => int(5)
}[5] => array(2) {
["id"] => int(6141)["rank"] => int(1)
}[6] => array(2) {
["id"] => int(6312)["rank"] => int(5)
}[7] => array(2) {
["id"] => int(6322)["rank"] => int(1)
}[8] => array(2) {
["id"] => int(6162)["rank"] => int(1)
}[9] => array(2) {
["id"] => int(6151)["rank"] => int(5)
}
}
}[1] => array(4) {
["id"] => int(35938366)["name"] => string(15)
"Thunderlord ADC" ["current"] => bool(false)["masteries"] => array(10) {
[0] => array(2) {
["id"] => int(6121)["rank"] => int(1)
}[1] => array(2) {
["id"] => int(6343)["rank"] => int(1)
}[2] => array(2) {
["id"] => int(6131)["rank"] => int(5)
}[3] => array(2) {
["id"] => int(6331)["rank"] => int(5)
}[4] => array(2) {
["id"] => int(6111)["rank"] => int(5)
}[5] => array(2) {
["id"] => int(6141)["rank"] => int(1)
}[6] => array(2) {
["id"] => int(6312)["rank"] => int(5)
}[7] => array(2) {
["id"] => int(6322)["rank"] => int(1)
}[8] => array(2) {
["id"] => int(6362)["rank"] => int(1)
}[9] => array(2) {
["id"] => int(6352)["rank"] => int(5)
}
}
}[2] => array(4) {
["id"] => int(35938367)["name"] => string(7)
"Twisted" ["current"] => bool(false)["masteries"] => array(10) {
[0] => array(2) {
["id"] => int(6121)["rank"] => int(1)
}[1] => array(2) {
["id"] => int(6114)["rank"] => int(5)
}[2] => array(2) {
["id"] => int(6131)["rank"] => int(5)
}[3] => array(2) {
["id"] => int(6343)["rank"] => int(1)
}[4] => array(2) {
["id"] => int(6331)["rank"] => int(5)
}[5] => array(2) {
["id"] => int(6142)["rank"] => int(1)
}[6] => array(2) {
["id"] => int(6312)["rank"] => int(5)
}[7] => array(2) {
["id"] => int(6322)["rank"] => int(1)
}[8] => array(2) {
["id"] => int(6351)["rank"] => int(5)
}[9] => array(2) {
["id"] => int(6362)["rank"] => int(1)
}
}
}[3] => array(4) {
["id"] => int(35938368)["name"] => string(13)
"CaptainJungle" ["current"] => bool(false)["masteries"] => array(10) {
[0] => array(2) {
["id"] => int(6121)["rank"] => int(1)
}[1] => array(2) {
["id"] => int(6343)["rank"] => int(1)
}[2] => array(2) {
["id"] => int(6114)["rank"] => int(5)
}[3] => array(2) {
["id"] => int(6331)["rank"] => int(5)
}[4] => array(2) {
["id"] => int(6321)["rank"] => int(1)
}[5] => array(2) {
["id"] => int(6141)["rank"] => int(1)
}[6] => array(2) {
["id"] => int(6312)["rank"] => int(5)
}[7] => array(2) {
["id"] => int(6134)["rank"] => int(5)
}[8] => array(2) {
["id"] => int(6362)["rank"] => int(1)
}[9] => array(2) {
["id"] => int(6352)["rank"] => int(5)
}
}
}[4] => array(4) {
["id"] => int(35938369)["name"] => string(6)
"TeeTop" ["current"] => bool(false)["masteries"] => array(10) {
[0] => array(2) {
["id"] => int(6343)["rank"] => int(1)
}[1] => array(2) {
["id"] => int(6131)["rank"] => int(5)
}[2] => array(2) {
["id"] => int(6122)["rank"] => int(1)
}[3] => array(2) {
["id"] => int(6331)["rank"] => int(5)
}[4] => array(2) {
["id"] => int(6111)["rank"] => int(5)
}[5] => array(2) {
["id"] => int(6312)["rank"] => int(5)
}[6] => array(2) {
["id"] => int(6142)["rank"] => int(1)
}[7] => array(2) {
["id"] => int(6322)["rank"] => int(1)
}[8] => array(2) {
["id"] => int(6351)["rank"] => int(5)
}[9] => array(2) {
["id"] => int(6362)["rank"] => int(1)
}
}
}[5] => array(4) {
["id"] => int(35938370)["name"] => string(13)
"ThreshShields" ["current"] => bool(false)["masteries"] => array(10) {
[0] => array(2) {
["id"] => int(6223)["rank"] => int(1)
}[1] => array(2) {
["id"] => int(6241)["rank"] => int(1)
}[2] => array(2) {
["id"] => int(6343)["rank"] => int(1)
}[3] => array(2) {
["id"] => int(6312)["rank"] => int(5)
}[4] => array(2) {
["id"] => int(6322)["rank"] => int(1)
}[5] => array(2) {
["id"] => int(6332)["rank"] => int(5)
}[6] => array(2) {
["id"] => int(6212)["rank"] => int(5)
}[7] => array(2) {
["id"] => int(6231)["rank"] => int(5)
}[8] => array(2) {
["id"] => int(6363)["rank"] => int(1)
}[9] => array(2) {
["id"] => int(6352)["rank"] => int(5)
}
}
}
}
}
}


And I wanna see if the value "Fervor ADC" for example exists in it, in order to use it for a membership verification, any way I can look for it using PHP knowing that the array can have more values, the [0] to [4] here are the number of pages and it varies from 1 to 20.
Thank you very much.

Answer

//Will comes to know value exist or not in array

echo in_array_r("Fervor ADC", $array) ? 'found' : 'not found';

//function to find a value exist or not in array.

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))) {
            return true;
        }
    }

    return false;
}