如何从php中的json获取嵌套值

时间:2018-09-27 01:34:16

标签: php json

我有一个要用作数据库的json文件,仅仅是因为我比mysql php更喜欢json的结构,所以我有以下代码

$query = "people.json";
$json = file_get_contents($query);
$data = json_decode($json, true);
$faq = $data['FAQ'];
$questions = $faq['Questions']['Number'];
foreach($questions as $question){
    $number = $question['number'];
    $question = $question['question'];
    $answer = $question['answer'];
?>

{
    "FAQ":{
        "Questions":{
            "Number":{
                "1":{
                    "number":"one",
                    "question":"How do i trade with Marketwh steam bots?",
                    "answer":"You can Trade one of 2 ways <br> Send a Trade offer to one of our Steam Bots <br> & #8226; find the item on our site see the price of it and then click trade and send a trade offer like normal."
                },
                "2":{
                    "number":"two",
                    "question":"What is a Steam Bot?",
                    "answer":"A Steam Bot is an automated Trading Bot that will automatcally accept offers sent to it that are within the item price range of buying or selling."
                },
                "3":{
                    "number":"three",
                    "question":"What is a Steam Bot?",
                    "answer":"A Steam Bot is an automated Trading Bot that will automatcally accept offers sent to it that are within the item price range of buying or selling."
                },
                "4":{
                    "number":"four",
                    "question":"",
                    "answer":""
                },
                "5":{
                    "number":"five",
                    "question":"",
                    "answer":""
                }
            }
        }
    }
}

$ number输出“一个”

$ question输出问题

$ answer输出问题变量中的第一个字母,第一个字母为“ H”,第二个和第三个答案为“ W”。 如您在上面的代码中看到的,我将它放在foreach中,它应该在FAQ数组中的每个数字1-4之间循环。 我正在使用它来为数据库调用php,我可以通过我的网站编辑和更改内容,而FAQ是我将要使用的FAQ页面

3 个答案:

答案 0 :(得分:1)

$ questions是一个对象,而不是列表。我希望您需要更改json格式,以便问题在列表中。如下所示:

{
  "FAQ": {
    "Questions": {
      "Number": [
        {
          "number": "one",
          "question": "How do i trade with Marketwh steam bots?",
          "answer": "You can Trade one of 2 ways <br> Send a Trade offer to one of our Steam Bots <br> & #8226; find the item on our site see the price of it and then click trade and send a trade offer like normal."
        },
        {
          "number": "two",
          "question": "What is a Steam Bot?",
          "answer": "A Steam Bot is an automated Trading Bot that will automatcally accept offers sent to it that are within the item price range of buying or selling."
        },
        {
          "number": "three",
          "question": "What is a Steam Bot?",
          "answer": "A Steam Bot is an automated Trading Bot that will automatcally accept offers sent to it that are within the item price range of buying or selling."
        },
        {
          "number": "four",
          "question": "",
          "answer": ""
        },
        {
          "number": "five",
          "question": "",
          "answer": ""
        }
      ]
    }
  }
}

答案 1 :(得分:0)

user3720435 所述,更新您的JSON,以使Number成为array ([])而不是Object ({})。您无法像您想要的那样遍历对象,但是,这可以通过数组来实现。有关您特定问题的信息,请参见以下内容。

https://so.amarokstudios.com/NestedJSONValues/

答案 2 :(得分:0)

我在原始问题中有疑问

$query = "people.json";
$json = file_get_contents($query);
$data = json_decode($json, true);
$faq = $data['FAQ'];
$questions = $faq['Questions']['Number'];
foreach($questions as $question){
    $number = $question['number'];
    $question = $question['question'];
    $answer = $question['answer'];
?>

我这样更改了php中的代码

$data = json_decode($json, true);
$questions = $data['FAQ']['Questions']['Number'];
foreach($questions as $quest){
    $number = $quest['number'];
    $question = $quest['question'];
    $answer = $quest['answer'];
?>

我在foreach中调用了$ question,然后我将foreach中的$ question更改为$ quest并对其进行了修复,一旦我意识到它可能会与原始方式冲突并且将json转换为原始方式,问了一下,我确实尝试使用[{}]方法将其作为数组来完成,但是它需要一个对象,输出就像我要感谢答案一样,很高兴我发现了什么问题