使用 php 循环嵌套 JSON

问题描述 投票:0回答:5

我有一个具有以下结构的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 解码来输出这些数据,但我不确定如何实现我想要的。我试图将此数据作为每个项目的每个 location_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 有限,我找不到任何似乎可以实现此目的的东西。

谢谢。

php json nested
5个回答
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
投票

假设你的 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 

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/>";
        }
    }
?>

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>';
    }
}

0
投票

将 JSON 数据添加到 $json 变量中。如果您有任何其他疑问,请查看此示例

<?php $data = json_decode($json, true); foreach ($data as $entry) { $item = $entry['Item']; $name = $item['name']; $class = $item['schedule']['class']; $uic = $item['schedule']['uic']; $days = $item['days']; $branding = $item['segment']['branding']; $transaction = $item['transaction']; foreach ($item['segment']['location'] as $location) { $locationType = $location['location_type']; $time = $location['time']; echo "$name, $class, $uic, $days, $branding, $locationType, $time, $transaction<br>"; } } ?>
    
© www.soinside.com 2019 - 2024. All rights reserved.