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

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里