使用php循环嵌套JSON

时间:2015-01-16 22:45:56

标签: php json nested

我有一个具有以下结构的json文件:

[{
    "Item": {
        "name": "Item 1",
        "schedule": {
            "class": "Class",
            "uic": "UIC"
        },
        "days": "Mondays",
        "segment": {
            "branding": "A1",
            "location": [{
                "location_type": "L1",
                "time": "18:00"
            }, {
                "location_type": "L2",
                "time": "18:15"
            }, {
                "location_type": "L3",
                "time": "18:18"
            }]
        },
        "transaction": "Trans"
}
}, {        "Item": {
        "name": "Item 2",
        "schedule": {
            "class": "Class",
            "uic": "UIC"
        },
        "days": "Tuesdays",
        "segment": {
            "branding": "A2",
            "location": [{
                "location_type": "Lu1",
                "time": "19:00"
            }, {
                "location_type": "L2",
                "time": "19:15"
            }, {
                "location_type": "L3",
                "time": "19:18"
            }, {
                "location_type": "L4",
                "time": "19:25"
            }]
        },
        "transaction": "Trans"
    }
}]

我正在使用php的json解码来输出这些数据,但我不确定如何实现我想要的。我试图将这些数据作为每个item_type值的新行,基本列出每个项目的每个组合。

以下示例基本上是我要输出的内容:

Item 1, Class, UIC, Mondays, A1, L1, 18:00, Trans
Item 1, Class, UIC, Mondays, A1, L2, 18:15, Trans
Item 1, Class, UIC, Mondays, A1, L3, 18:17, Trans
Item 2, Class, UIC, Tuesdays, A2, L1, 19:00, Trans
Item 2, Class, UIC, Tuesdays, A2, L2, 19:15, Trans
Item 2, Class, UIC, Tuesdays, A2, L3, 19:17, Trans
Item 2, Class, UIC, Tuesdays, A2, L4, 19:25, Trans

我假设我需要深入到该位置,然后为每个项目循环浏览它,但我的php是有限的,我无法找到任何似乎可以实现此目的。

感谢。

4 个答案:

答案 0 :(得分:1)

如果您的JSON是正确的(并且不是),则代码如下:

foreach($JSON as $item)
{
    $item = $item->Item; // because you changed the code

    for($i=0;$i<count($item->segment->location);$i++)
    {
        echo $item->name, ', ';
        echo $item->schedule->class. ', ';
        echo $item->schedule->uic. ', ';
        echo $item->days. ', ';
        echo $item->segment->branding. ', ';
        echo $item->segment->location[$i]->location_type. ', ';
        echo $item->segment->location[$i]->time. ', ';
        echo $item->segment->transaction. '<br>';
    }
}

答案 1 :(得分:1)

假设你的json是变量$ json

$array = json_decode($json);

foreach ($array as $value) {
    foreach ($value->Item->segment->location as $loc) {
        echo sprintf('%s, %s, %s, %s, %s, %s, %s, %s <br>', 
            $value->Item->name,
            $value->Item->schedule->class,
            $value->Item->schedule->uic,
            $value->Item->days,
            $value->Item->segment->branding,
            $loc->location_type,
            $loc->time,
            $value->Item->transaction
        );
    }
}

这将输出

Item 1, Class, UIC, Mondays, A1, L1, 18:00, Trans 
Item 1, Class, UIC, Mondays, A1, L2, 18:15, Trans 
Item 1, Class, UIC, Mondays, A1, L3, 18:18, Trans 
Item 2, Class, UIC, Tuesdays, A2, Lu1, 19:00, Trans 
Item 2, Class, UIC, Tuesdays, A2, L2, 19:15, Trans 
Item 2, Class, UIC, Tuesdays, A2, L3, 19:18, Trans 
Item 2, Class, UIC, Tuesdays, A2, L4, 19:25, Trans 

答案 2 :(得分:0)

<?php
    $input = '[
        {
            "Item": {
                "name": "Item 1",
                "schedule": {
                    "class": "Class",
                    "uic": "UIC"
                },
                "days": "Mondays",
                "segment": {
                    "branding": "A1",
                    "location": [
                        {
                            "location_type": "L1",
                            "time": "18:00"
                        },
                        {
                            "location_type": "L2",
                            "time": "18:15"
                        },
                        {
                            "location_type": "L3",
                            "time": "18:18"
                        }
                    ]
                },
                "transaction": "Trans"
            }
        },
        {
            "Item": {
                "name": "Item 2",
                "schedule": {
                    "class": "Class",
                    "uic": "UIC"
                },
                "days": "Tuesdays",
                "segment": {
                    "branding": "A2",
                    "location": [
                        {
                            "location_type": "Lu1",
                            "time": "19:00"
                        },
                        {
                            "location_type": "L2",
                            "time": "19:15"
                        },
                        {
                            "location_type": "L3",
                            "time": "19:18"
                        },
                        {
                            "location_type": "L4",
                            "time": "19:25"
                        }
                    ]
                },
                "transaction": "Trans"
            }
        }
    ]';

    $items = json_decode($input);

    foreach ($items as $item) {
        $item = $item->Item;
        for ($i = 0, $l = count($item->segment->location); $i < $l; $i++ ) {
            echo $item->name . ", ";
            echo $item->schedule->class . ", ";
            echo $item->schedule->uic . ", ";
            echo $item->days . ", ";
            echo $item->segment->branding . ", ";
            echo $item->segment->location[$i]->location_type . ", ";
            echo $item->segment->location[$i]->time . ", ";
            echo $item->transaction;
            echo "<br/>";
        }
    }
?>

答案 3 :(得分:0)

 $jsonStr = <<<JSON
[{
    "Item": {
        "name": "Item 1",
        "schedule": {
            "class": "Class",
            "uic": "UIC"
        },
        "days": "Mondays",
        "segment": {
            "branding": "A1",
            "location": [{
                "location_type": "L1",
                "time": "18:00"
            }, {
                "location_type": "L2",
                "time": "18:15"
            }, {
                "location_type": "L3",
                "time": "18:18"
            }]
        },
        "transaction": "Trans"
}
}, {        "Item": {
        "name": "Item 2",
        "schedule": {
            "class": "Class",
            "uic": "UIC"
        },
        "days": "Tuesdays",
        "segment": {
            "branding": "A2",
            "location": [{
                "location_type": "Lu1",
                "time": "19:00"
            }, {
                "location_type": "L2",
                "time": "19:15"
            }, {
                "location_type": "L3",
                "time": "19:18"
            }, {
                "location_type": "L4",
                "time": "19:25"
            }]
        },
        "transaction": "Trans"
    }
}]
JSON;

$arr = json_decode($jsonStr);


foreach($arr as $item)
{

    $locations = $item->Item->segment->location;
    foreach($locations as $loc)
    {
        echo $item->Item->name, ', ';
        echo $item->Item->schedule->class. ', ';
        echo $item->Item->schedule->uic. ', ';
        echo $item->Item->days. ', ';
        echo $item->Item->segment->branding. ', ';
        echo $loc->location_type. ', ';
        echo $loc->time. ', ';
        echo $item->Item->transaction. '<br>';
    }
}