donglvlao8367 2019-03-03 05:40
浏览 263
已采纳

使用php动态解析多个json数组对象

I have a json file as below name "brands.json"

{
   "title":"List of brands",
   "version" : 1,
   "nike": [
    {"type":"shoes","size":10,"color":"black"},
    {"type":"shirt","size":"S","color":"black"}
   ],
   "converse": [
    {"type":"shoes","size":10,"color":"red"},
    {"type":"backpack","size":"N/A","color":"red"}
   ],
   "champion": [
    {"type":"shoes","size":10,"color":"blue"},
        {"type":"pants","size":"M","color":"grey"}      
    ]
}

I looked at some example online and get this piece of code

<?php
        $read = file_get_contents("report.json");
        $json = json_decode($read, true);
        foreach($json as $key => $val){
            if(is_array($val)){
                echo "$key : <br>";
                foreach($key as $k => $v){
                    echo "$k => $v<br>";
                }
            }       
            else {
                echo "$key => $val<br>";            
            }
        }
    ?>

I would be able to print out

title => List of brands

version => 1

nike :

converse :

champion :

But I would like to get the array inside of those brands. I was thinking of having a foreach loop inside the if statement. However, it returns errors

nike : Warning: Invalid argument supplied for foreach().

Some resources suggested to do something like $json->nike as $item => $v but that will be redundant since I also have converse and champion arrays. If someone could direct me to a good resource or provide a code example, it'd be very much appreciated.

Expected table Nike:

type | size | color

shoes| 10 | black

shirt| S | black

  • 写回答

1条回答 默认 最新

  • dq05304 2019-03-03 06:22
    关注

    Create function that buffers output and display it

        $read = '{
           "title":"List of brands",
           "version" : 1,
           "nike": [
            {"type":"shoes","size":10,"color":"black"},
            {"type":"shirt","size":"S","color":"black"}
           ],
           "converse": [
            {"type":"shoes","size":10,"color":"red"},
            {"type":"backpack","size":"N/A","color":"red"}
           ],
           "champion": [
            {"type":"shoes","size":10,"color":"blue"},
                {"type":"pants","size":"M","color":"grey"}      
            ]
        }';
    
        $json = json_decode($read, true);
    
        // this function will run recursively
        function display($val){
           // create output buffer variable
            $output = "";
           // check is array or not
            if(is_array($val)){
                // check is multimensional or not
                if(isset($val[0])){
                    foreach($val as $v){
                        $output .= "<br/>".display($v);
                    }
                }else{
                    foreach($val as $k => $v){
                        $output .= $k." => ".display($v)."<br/>";
                    }
                }      
            }else{
                // append output if it just a value
                $output .= $val;
            }
            return $output;
        }
    
        echo display($json);
    
    ?>
    

    Preview https://code.sololearn.com/wkeim4HQO9zP#php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容