dongxing8766 2018-09-27 01:34
浏览 155
已采纳

如何从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 2018-09-27 05:15
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题