简体   繁体   English

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

[英]How to get nested values from json in php

I have a json file i wanted to use as a database simply because I like the structure of json than mysql php i have the following code我有一个 json 文件我想用作数据库只是因为我喜欢 json 的结构而不是 mysql php 我有以下代码

$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 outputs "one" $number 输出“一”

$question outputs the question $question 输出问题

$answer outputs the first letter in the question variable for the first one it is "H" and the second and third answer it is a "W". $answer 输出问题变量中的第一个字母,第一个是“H”,第二个和第三个答案是“W”。 as you can see in the code above i have it in a foreach which should cycle through each of the numbers 1-4 in the FAQ array正如你在上面的代码中看到的,我把它放在一个 foreach 中,它应该循环遍历 FAQ 数组中的每个数字 1-4

I am using this to call in php for the database i can just edit and change the stuff with my website and FAQ is the FAQ page i will be using我正在使用它来调用数据库的 php 我可以编辑和更改我的网站的内容,常见问题是我将使用的常见问题页面

$questions is an object, not a list. $questions 是一个对象,而不是一个列表。 I expect you need to change your json format so the questions are in a list.我希望您需要更改 json 格式,以便将问题列在列表中。 Like the following:像下面这样:

{
  "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": ""
        }
      ]
    }
  }
}

As user3720435 said, update your JSON to make Number an array ([]) instead of an Object ({}) .正如user3720435所说,更新您的 JSON 以使Number成为array ([])而不是Object ({}) You are not able to iterate over objects like you're wanting, however, this can be achieved with an array.您无法像您想要的那样迭代对象,但是,这可以通过数组来实现。 Please see the following for information regarding your specific question.有关您的具体问题的信息,请参阅以下内容。

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

I had in the Original question我在原始问题中

$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'];
?>

I changed the code in the php like this我像这样更改了php中的代码

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

i had called the $question in the foreach and once after that i changed the $question in the foreach to $quest and it fixed it, once i realized that it was probably conflicting like that and the json to the original way i asked about and i did try the [{}] way to do it as an array but it needed an object and the out put is just like how i wanted thanks for the answers and i am glad i figured out what was wrong我在 foreach 中调用了 $question ,之后我将 foreach 中的 $question 更改为 $quest 并修复了它,一旦我意识到它可能像那样和 json 冲突,我问的原始方式和我确实尝试过 [{}] 方法将它作为一个数组来做,但它需要一个对象,输出就像我想要的一样感谢您的答案,我很高兴我发现出了什么问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM