Foreach循环JSON获取特定值 [英] Foreach Loop JSON get specific value

查看:97
本文介绍了Foreach循环JSON获取特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坐在一个小问题的前面. 现在,我创建一个魔兽世界页面,您可以在其中追踪游戏中的多个统计数据.

我与暴雪的API的工作,并希望得到一个特定的值了这款本的不能这样做..

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

..因为行[2207]不是静态的并且可以更改.因此,我需要在成就"的每2392行中搜索ID"13212".我如何以目前的方式实现这一目标?当我想在每一行中搜索时,我需要写什么而不是行[2207].因为它的变量?

感谢您的帮助. 我希望我解释清楚,我的英语不是最好的.

一些JSON示例,如果上面的链接不起作用:

  "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
},

解决方案

好,我得到了@Beniamin的解决方案.

我将您的代码更改为此(杀死了"continue;",并将!==更改为==):

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

对我有用.

感谢大家的帮助!

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

解决方案

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!

这篇关于Foreach循环JSON获取特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆