使用核心php显示来自foreach的json数据

时间:2017-06-05 06:56:35

标签: php json

这是json数据:

{"activities":[{"id":"23442","title":"coldplay","author":"ekkk@abc.com","modified":"due","action":"updated","date":"2017-05-31 12:59:27","message":"Updated the due date to 2017-07-12 10:30:00"},{"id":"23650","title":"Task For Timesheet viewer ","author":"tkkl@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-31 09:51:35","message":"Accepted task"},{"id":"23650","title":"Task For Timesheet viewer ","author":"ekkk@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-31 09:50:55","message":"Accepted task"},{"id":"23607","title":"project 3","author":"ekkk@abc.com","modified":"status","action":"updated","date":"2017-05-30 15:40:11","message":"Deleted task"},{"id":"23645","title":"naiman bug 2","author":"ekkk@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-29 11:58:38","message":"Accepted task"},{"id":"23645","title":"naiman bug 2","author":"nkkk@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-29 11:58:07","message":"Accepted task"}]}

要在表格中查看此数据,我在视图文件中使用了foreach循环。但它无法显示,警告:为foreach()提供的参数无效。这就是我的做法

<tbody>
            <tr>
            <?php foreach($notifications as $key => $value) {     ?>
                        <td><?php echo $value ?></td>
                    <?php } ?>
            </tr>
        </tbody>

3 个答案:

答案 0 :(得分:1)

在使用foreach打印数据后,必须使用json_deode将json转换为数组

   $notification = json_decode('{"activities":[{"id":"23442","title":"coldplay","author":"ekkk@abc.com","modified":"due","action":"updated","date":"2017-05-31 12:59:27","message":"Updated the due date to 2017-07-12 10:30:00"},{"id":"23650","title":"Task For Timesheet viewer ","author":"tkkl@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-31 09:51:35","message":"Accepted task"},{"id":"23650","title":"Task For Timesheet viewer ","author":"ekkk@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-31 09:50:55","message":"Accepted task"},{"id":"23607","title":"project 3","author":"ekkk@abc.com","modified":"status","action":"updated","date":"2017-05-30 15:40:11","message":"Deleted task"},{"id":"23645","title":"naiman bug 2","author":"ekkk@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-29 11:58:38","message":"Accepted task"},{"id":"23645","title":"naiman bug 2","author":"nkkk@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-29 11:58:07","message":"Accepted task"}]}');

foreach ($notification->activities as $key => $value)
{
    echo $value->id."<br>";
    echo $value->title."<br>";
}

或者只是将第二个参数添加为json,然后将json转换为关联数组,这样就可以像使用foreach一样使用它了

eg $notification = json_deocde('your_json',true);

现在你可以将它用作你的foreach

答案 1 :(得分:1)

将其用作

<tbody>
        <tr>
        <?php 
$notifications = json_decode('{"activities":[{"id":"23442","title":"coldplay","author":"ekkk@abc.com","modified":"due","action":"updated","date":"2017-05-31 12:59:27","message":"Updated the due date to 2017-07-12 10:30:00"},{"id":"23650","title":"Task For Timesheet viewer ","author":"tkkl@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-31 09:51:35","message":"Accepted task"},{"id":"23650","title":"Task For Timesheet viewer ","author":"ekkk@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-31 09:50:55","message":"Accepted task"},{"id":"23607","title":"project 3","author":"ekkk@abc.com","modified":"status","action":"updated","date":"2017-05-30 15:40:11","message":"Deleted task"},{"id":"23645","title":"naiman bug 2","author":"ekkk@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-29 11:58:38","message":"Accepted task"},{"id":"23645","title":"naiman bug 2","author":"nkkk@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-29 11:58:07","message":"Accepted task"}]}');
foreach($notifications->activities as $key => $value) {     ?>
                    <td><?php  echo $value->message;//or whatever you like  ?></td>
                <?php } ?>
        </tr>
    </tbody>

答案 2 :(得分:0)

您正在尝试打印一个对象,这不是一件正确的事情。您应该打印此对象包含的标量。

$s = '{"activities":[{"id":"23442","title":"coldplay","author":"ekkk@abc.com","modified":"due","action":"updated","date":"2017-05-31 12:59:27","message":"Updated the due date to 2017-07-12 10:30:00"},{"id":"23650","title":"Task For Timesheet viewer ","author":"tkkl@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-31 09:51:35","message":"Accepted task"},{"id":"23650","title":"Task For Timesheet viewer ","author":"ekkk@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-31 09:50:55","message":"Accepted task"},{"id":"23607","title":"project 3","author":"ekkk@abc.com","modified":"status","action":"updated","date":"2017-05-30 15:40:11","message":"Deleted task"},{"id":"23645","title":"naiman bug 2","author":"ekkk@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-29 11:58:38","message":"Accepted task"},{"id":"23645","title":"naiman bug 2","author":"nkkk@abc.com","modified":"assigneeView","action":"updated","date":"2017-05-29 11:58:07","message":"Accepted task"}]}';

$data = json_decode ($s);
$notifications = $data->activities;

foreach($notifications as $key => $notification) {
    echo ($notification->id);
}