GET request array to table in PHP

倾然丶 夕夏残阳落幕 提交于 2021-02-08 11:57:53

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/45195400/get-request-array-to-table-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!