1

I have a json file with the following structure:

[{
    "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"
    }
}]

I am using php's json decode to out put this data, but I'm not sure how to achieve what I want. I am trying to out put this data as a new line for every location_type value for each item, basically listing out every combination for each item.

The example below is essentially what I am looking to output:

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

I'm assuming I need to drill down to the location and then loop through it for each item, but my php is limited and I can't find anything that seems to achieve this.

Thanks.

3
  • 1
    Can you share what you've tried? Commented Jan 16, 2015 at 22:46
  • 2
    I can be wrong, but your JSON seems incorrect. It's an array with one big object inside. Which has two properties, and each of them has name "Item". Which is incorrect. Commented Jan 16, 2015 at 22:50
  • @OlegDubas You're absolutely right, I have corrected the code. Commented Jan 16, 2015 at 22:58

4 Answers 4

1

If your JSON would be correct (and it is not), the code would be like:

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>';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

assume your json are on variable $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
        );
    }
}

this will output

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 

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.