user2796515
user2796515

Reputation: 264

GET request array to table in PHP

I'm trying to create an html table based on the results from an array from a GET request. I have tried for loops and I have tried Java examples, but the results are always displayed as a long string (or if I return the results as dd($response) it only returns one row. I was wondering if there is a problem with the way format the array is returned:

{   "results":
[

    {   
        "column1":"TEST1",
        "column2":"DATADATADATA",   
        "time":"2017-02-27T16:25:03.1230000Z"
    },
    {   
        "column1":"TEST2",
        "column2":"DATADATADATA",
        "time":"2017-07-03T02:48:29.8300000Z"
    },
    {
        "column1":"TEST3",
        "column2":"DATADATADATA",
        "time":"2017-07-19T15:09:27.0900000Z"}
]

}

This is one example I tried in PHP:

$reponse = array(print_r($response));


for ($i = 0; $i < count($reponse); $i++) {
    for ($l = 0; $l < count($reponse[$i]); $l++) {
        echo $reponse[$i][$l];
        echo "<br/>";
    };
};

Upvotes: 0

Views: 173

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

Use json_decode() to convert JSON to an array:

$array = json_decode($data, true);

Then you'll be able to iterate over it:

@foreach ($array['results'] as $element)
    {{ $element['column1'] }}
    {{ $element['column2'] }}
    {{ $element['time'] }}
@endforeach

Upvotes: 2

Related Questions