在JSON数组中搜索匹配键

时间:2017-07-17 22:42:17

标签: php arrays json sorting

我试图搜索json数组并查找包含奖牌一词的所有键,即......

  • medalsActivityCompleteHighestScoreWinning
  • medalsActivityCompleteVictoryBlowout
  • medalsKillHeadshot
  • medalsKillMulti2
  • medalsKillSpree1
  • medalsKillSpree2
  • medalsWinningScore
  • medalsWeaponPulseRifleKillSpree
  • medalsPaybackKill
  • medalsAvenger

我试过......

$x = '0';
foreach ($json['Response']['data']['entries']['0'] as $extended) {
    $valueName[] = $extended['values'];

    if (strpos($valueName[$x], 'medals') !== false) {
        echo $valueName[$x];
    }

    $x++;
}

这是数组

Array
(
[Response] => Array

        [data] => Array

                [period] => 2017-07-17T21:11:58Z
                [activityDetails] => Array
                [entries] => Array

                        [0] => Array

                                [standing] => 0
                                [score] => Array
                                [player] => Array
                                [characterId] => 2305843009334005960
                                [values] => Array
                                [extended] => Array

                                        [weapons] => Array
                                        [values] => Array
                                                                                                 [totalKillDistance] => Array
                                                [kills] => Array
                                                [averageKillDistance] => Array
                                                [secondsPlayed] => Array
                                                [deaths] => Array
                                                [averageLifespan] => Array
                                                [score] => Array
                                                [averageScorePerKill] => Array
                                                [averageScorePerLife] => Array                                                    
                                                [killsOfPlayerWarlock] => Array
                                                [medalsActivityCompleteHighestScoreWinning] => Array
                                                [medalsActivityCompleteVictoryBlowout] => Array
                                                [medalsKillHeadshot] => Array
                                                [medalsKillMulti2] => Array
                                                [medalsKillSpree1] => Array
                                                [medalsKillSpree2] => Array
                                                [medalsWinningScore] => Array
                                                [medalsWeaponPulseRifleKillSpree] => Array
                                                [medalsPaybackKill] => Array
                                                [medalsAvenger] => Array
                                                [allMedalsScore] => Array
                                                [allMedalsEarned] => Array
                                                [precisionKills] => Array
                                                [weaponPrecisionKillsPulseRifle] => Array
                                                [weaponKillsGrenade] => Array
                                                [weaponKillsMelee] => Array
                                                [weaponKillsPulseRifle] => Array
                                                [weaponKillsRocketLauncher] => Array
                                                [weaponKillsSuper] => Array
                                                [weaponBestType] => Array
                                                [weaponKillsPrecisionKillsPulseRifle] => Array
                                                [allParticipantsCount] => Array
                                                [allParticipantsScore] => Array
                                                [allParticipantsTimePlayed] => Array
                                                [fireTeamId] => Array
                                                [longestKillSpree] => Array
                                                [longestSingleLife] => Array
                                                [orbsDropped] => Array
                                                [totalActivityDurationSeconds] => Array
                                                [dailyMedalsEarned] => Array
                                                [combatRating] => Array
                                                [sparksCaptured] => Array
                                                [slamDunks] => Array
                                                [dunkKills] => Array

1 个答案:

答案 0 :(得分:3)

应该这样做:

foreach ($json['Response']['data']['entries'][0]['extended']['values'] as $key => $value) {
  if (false !== strpos($key, 'medals')) {
      echo $value.PHP_EOL;
  }
}

或者,您可以使用array_filter

var_dump(array_filter($json['Response']['data']['entries'][0]['extended']['values'], function ($k, $v) {
    return false !== strpos($k, 'medals');
}, ARRAY_FILTER_USE_BOTH));