php – Foreach Loop JSON get specific value-ThrowExceptions

Exception or error:

im sitting in front of a little problem.
For now i create a World of Warcraft-Page where you can track multiple stats in the game.

I work with the Blizzard API and want to get a specific value out of this this json-file.
I thought, i could realise it with a foreach loop which is looking in the “achievements” tree for the ID “13212”, the problem is: In “achievements” tree, there are over 2000 numbered rows and the Row where the ID is in, is variable. So i cant do it like that..

  foreach($decodeachieve['achievements'][2207]['id'] as $item) {
        if($item == "13212") {
        //show Achievementpicture 
          }
        }

..because the row [2207] is not static and can change. So i need to search for id “13212” in every 2392 Rows of “achievements”. How do i realise this in the way i made for now? What do i need to write instead of the row [2207] when i want to search in every row. because its variable ?

Thanks for any help.
I hope I explained it clearly, my english is not the best.

Some JSON Example, if link above doesnt work:

  "achievements": [
{
  "id": 13212,
  "achievement": {
    "key": {
      "href": "https://us.api.blizzard.com/data/wow/achievement/6?namespace=static-8.3.0_32861-us"
    },
    "name": "Level 10",
    "id": 13212
  },
  "criteria": {
    "id": 2050,
    "is_completed": true
  },
  "completed_timestamp": 1313210612000
},
{
  "id": 7,
  "achievement": {
    "key": {
      "href": "https://us.api.blizzard.com/data/wow/achievement/7?namespace=static-8.3.0_32861-us"
    },
    "name": "Level 20",
    "id": 7
  },
  "criteria": {
    "id": 2683,
    "is_completed": true
  },
  "completed_timestamp": 1313215978000
},
How to solve:

Easiest way is just split the foreach into two:

    foreach($decodeachieve['achievements'] as $achievement) {
        // first go through all achievements
        foreach ($achievement as $item) {
            //then you should be able to filter items
            if ($item !== "13212") {
                continue;
            }
            // do something
        }
    }

Edit: after you posted the json it seems to be much more easier:

foreach($decodeachieve['achievements'] as $achievement) {
      if ($achievement['id'] !== "13212") { // or $achievement['id']['achievement']
            continue;
      }
     // do something
}

Answer´╝Ü

Ok, i got the solution @Beniamin.

I changed your code to this (killed “continue;” and changed !== to ==):

        foreach($decodeachieve['achievements'] as $achievement) {
        if ($achievement['id'] == "5266") { // or $achievement['id']['achievement']

        echo "<div class='specialTitles'><img src='https://wow.zamimg.com/images/wow/icons/large/achievement_arena_3v3_7.jpg' style='border-radius: 50%; border: 2px solid #fff; box-shadow: 0px 0px 11px 1px rgba(0,0,0,0.37);'>";
        echo "<div class='document__title'>Dreierbande: 2.400</div></div>";
        }   
}

That worked for me.

Thanks for your help @ all!

Leave a Reply

Your email address will not be published. Required fields are marked *