donglvlao8367
2019-03-03 05:40 阅读 210

使用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 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

    点赞 评论 复制链接分享

相关推荐