如何在php中显示json文件中的数据?

时间:2016-10-27 06:32:51

标签: php json

我现在在json文件 abc.json 中有这些数据,如何显示和slso数据(id& name)这个文件并在php中显示它们,但是分开。这只是一个例子。我有很多数据。

       [{
            "Car": [{
                    "id": "1",
                    "name": "Bus"
                }, {
                    "id": "2",
                    "name": "Truck"
                }]
        },
        {
            "Fruit": [{
                    "id": "1",
                    "name": "Mango"
                }, {
                    "id": "2",
                    "name": "Pinapple"
                }]
        }]

2 个答案:

答案 0 :(得分:3)

只需获取文件内容,对其进行解码,然后打印:

<?php
    $content = json_decode(file_get_contents("abc.json"), true);
    print_r($content);
?>

答案 1 :(得分:0)

没有经过测试,但这应该会给你一些想法

/* Read the file and assign to a variable */
$data = file_get_contents( 'abc.json' );

/* Decode the json data and create an array */
$json = jsondecode( $data, true );

/* get the array keys */
$keys = array_keys( $json );

/* debug output */
var_dump( $keys );

/* process each node of the json data */
foreach( $json as $arr ){
    foreach( $arr as $obj ) echo $obj->id, $obj->name;
}
相关问题