dongxing8766
dongxing8766
2018-09-27 01:34

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

已采纳

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

$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"

$question outputs the 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". 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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dongzhi7763 dongzhi7763 3年前

    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

    $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

    点赞 评论 复制链接分享
  • dtlh12053 dtlh12053 3年前

    As user3720435 said, update your JSON to make Number an array ([]) instead of an 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/

    点赞 评论 复制链接分享
  • dpdfh60088 dpdfh60088 3年前

    $questions is an object, not a list. I expect you need to change your json format so the questions are in a list. 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": ""
            }
          ]
        }
      }
    }
    
    点赞 评论 复制链接分享

相关推荐