在PHP中查找多维数组中的值



我有这样一个数组:

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) }
                }
            }
        }
    }
}

我想看看其中是否存在具有特定"name"键的项目(例如"fever ADC"),以便将其用于成员资格验证。我如何寻找它知道数组可以有更多的值?这里的[0]到[4]是页数,取值范围从1到20。

//将会知道数组中的值是否存在

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

//查找数组中存在或不存在的值。

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;
}

初始化结果和搜索项

$result = null;
$searchName = "Fervor ADC"

遍历'pages'键中的值:

foreach ($your_array['36694730']['pages'] as $x) {
    // if you find one that matches your search term, set your result to that one
    if ($x['name'] == $searchName) {
        $result = $x;
        // then stop the loop; you're done.
        break;
    }
}

如果你只是想验证它是否存在

if ($result)

。如果您最终需要对与该名称相关的数据做任何其他操作,那么您将在$result中提供它。

相关内容

  • 没有找到相关文章

最新更新