简体   繁体   English

表格中显示多个数组的json数据

[英]json data with multiple arrays displaying in a table

i have a json data like so 我有一个像这样的json数据

{
    "code": 1,
    "data": [
        {
            "apple": [
                {
                    "id": 127,
                    "type": 1,
                    "color": green,                   
                    "stage": 1,                    
                    "status": 1                   
                },
                {
                    "id": 128,
                    "type": 2,
                    "color": red,                   
                    "stage": 1,                    
                    "status": 1  
                }
            ]
        },
        {
            "oranges": [
                {
                    "id":133
                   "type": 3,
                    "color": rainbow,                   
                    "stage": 1,                    
                    "status": 1   
                },
                {
                   "id":134
                   "type": 3,
                    "color": black,                   
                    "stage": 1,                    
                    "status": 1
                }
            ]
        },
        {
            "berry": [
                {
                    "id":4
                   "type": 2,
                    "color": white,                   
                    "stage": 1,                    
                    "status": 1
                }
            ]
        },
        {
            "watermelon": [
                {
                    "id":5
                   "type": 2,
                    "color": red and blue,                   
                    "stage": 1,                    
                    "status": 1
                }
            ]
        }
    ],
    "bleh": "Succesfully queried database"
}

i would like to create a table in php that goes somthing like this 我想在php中创建一个像这样的表

Fruit |  Type | color
apple    1     green
apple    2     red
oranges  3     rainbow
oranges  3     black

so basically what i want is when ever a object like apple has more then one array inside it the table to display apple and the corresponding data to it. 所以基本上我想要的是什么时候像Apple这样的对象中有一个以上的数组在表中显示apple及其对应的数据。 this is what i have so far 这就是我到目前为止

 $output = json_decode(JsonData);
     $result =$output['data'][0]['apple'];

<table>
    <thead> 
            <tr>                
                <th>Fruits</th> 
                <th>Type</th> 
                <th>color</th>                 
            </tr> 
            </thead> 
            <tbody> 
        <?php if(!isset($result)){ ?>
        <tr> 
            <td>Empty</td> 
            <td>Empty</td> 
            <td>Empty</td> 
            <td>Empty</td>       
        </tr> 
        <?php else{
            foreach($result as $apples){ ?>              
            <tr> 
                <td></td> 
                <td><?php echo "$apples["type"]";?></td> 
                <td><?php echo "$apples["color"]";?></td>                    
            </tr> 
          <?php } ?>
        <?php } ?>
            </tbody> 
        </table>

Well just at a glance this is completely wrong syntax 乍一看,这是完全错误的语法

<?php echo "apples["type"]";?>

Probably you need something like this 可能您需要这样的东西

<?php echo $apples["type"];?>

What you had will probably give you a syntax error because PHP will see it like this 您所拥有的可能会给您带来语法错误,因为PHP会这样看

<?php echo "apples["   type   "]";    ?>

Where "apples[" is a complete string, type is an undefined constant that is unexpected in the location it's in, and then an extra string "]"; 其中"apples["是一个完整的字符串, type是一个未定义的常量,在其所在的位置是意外的,然后是一个额外的字符串"]";

you can test it in this sandbox 您可以在此沙箱中对其进行测试

https://3v4l.org/nR8vF https://3v4l.org/nR8vF

Which gives us this 这给了我们

Parse error: syntax error, unexpected 'type' (T_STRING), expecting ',' or ';' in /in/nR8vF on line 5

UPDATE 更新

Well there are a few other obvious problems, just with this 好吧,还有其他一些明显的问题

$output = json_decode(JsonData);
$result = $output['data'][0]['apple'];

I assume JsonData is just a placeholder or are you missing the $ on that too? 我认为JsonData只是一个占位符,还是您也错过了$

Then as you don't have the second argument set to true, then data will be object style. 然后,由于没有将第二个参数设置为true,因此data将为对象样式。 Personally I would do 就我个人而言

 $output = json_decode(JsonData, true);

And just use it as an array. 并将其用作数组。

Others have covered the double "foreach" deal (for looping on fruit), so I won't re-hash that, I would have covered it but it was "Dinner" time and my wife gets a bit "Irritated" when I mess with code instead of coming up from the office (I have an office in the basement). 其他人涵盖了双重“ foreach”交易(用于循环水果),所以我不会再重复说明了,我会进行涵盖,但是那是“晚餐”的时间,当我一团糟时,我的妻子有点“恼怒”用代码而不是从办公室出来(我在地下室有一个办公室)。

$arr = json_decode($json, true); 
foreach($arr['data'] as $fruit => $types){
 foreach($types as $info){
 echo $fruit;
 echo $info['type']; 
 echo $info['color'];
}
}

This is how you can get your JSON data into PHP. 这是将JSON数据导入PHP的方法。 I trust you can create an HTML table or something to display this information. 我相信您可以创建一个HTML表或类似的东西来显示此信息。

If you are looking to show all fruits then you will need something along the lines of: 如果您希望展示所有水果,那么您将需要遵循以下要求:

$result =$output['data'];

Then update your foreach loop 然后更新您的foreach循环

foreach($result as $key=>$fruit){ 
    foreach($fruit as $k=>$dets){?>              
        <tr> 
            <td></td> 
            <td><?php echo $dets["type"];?></td> 
            <td><?php echo $dets["color"];?></td>                    
        </tr> 
<?php } 
} ?>

That's also not valid json 那也是无效的json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM