PHP'json_decode'仅适用于第一项 [英] PHP 'json_decode' Working for First Item Only

查看:87
本文介绍了PHP'json_decode'仅适用于第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解码JSON格式,我的API端点是 https://api.reliableserver.host/api/recent_activities (无身份验证)

I'm decoding JSON format, my API Endpoint is https://api.reliableserver.host/api/recent_activities (No Auth)

我已经设法像这样显示第一项的值:

I've already managed to display the values of the first item like this:

<?php
$url2 = "https://api.reliableserver.host/api/recent_activities";
$obj2 = json_decode(file_get_contents($url2), true);
?>

<tr class="css-xlbdcw">

    <td style="width: unset; max-width: unset; padding: 5px 20px; text-align: left;">
        <div class="vx_text-body-md" style="color: rgb(44, 46, 47); padding-top: 7px; padding-bottom: 7px;">
            <div role="button">
                <?php echo $obj2['data'][0]['activity_date']; ?>
            </div>
        </div>
    </td>

    <td style="width: unset; max-width: unset; padding: 5px 20px; text-align: left;">
    <div data-testid="verticalList">
        <a tabindex="0" href="/activity/payment/87J62844XL761054H"><?php echo $obj2['data'][0]['activity_title']; ?></a>
        <div style="color: rgb(44, 46, 47); font-size: 11px;"><?php echo $obj2['data'][0]['activity_status']; ?></div>
    </div>
    </td>

    <td style="width: unset; max-width: unset; padding: 5px 20px; text-align: right;">
        <div class="vx_text-body-md" style="color: rgb(44, 46, 47); padding-top: 7px; padding-bottom: 7px;">
            <div role="button"><?php echo $obj2['data'][0]['activity_amount']; ?></div>
        </div>
    </td>

</tr>

这适用于第一个项目,如何使用foreach使它遍历每个项目-我试过但无法正常工作.

This is working for the first item, how do i make it go through each item using foreach - I tried but couldn't get it working.

谢谢

推荐答案

如果要允许自己灵活地渲染项目,可以使用过滤器数组($filter),如下所示:

If you wanted to allow yourself flexibility in rendering items, you could use a filter array ($filter), as follows:

<?php
$response = file_get_contents('https://api.reliableserver.host/api/recent_activities');
$response = json_decode($response, true);
$filter = ['id', 'activity_amount']; // select which items to show
foreach ($response['data'] as $record) {
    echo '<tr class="css-xlbdcw">'; // open row
    foreach ($record as $key => $item) {
        if (in_array($key, $filter)) { // show filtered items only
            echo <<<HEREDOC
    <td style="width: unset; max-width: unset; padding: 5px 20px; text-align: left;">
        <div class="vx_text-body-md" style="color: rgb(44, 46, 47); padding-top: 7px; padding-bottom: 7px;">
            <div role="button">
                $item
            </div>
        </div>
    </td>
HEREDOC;
        }
    }
    echo '</tr>'; // close row
}

我们使用 HEREDOC ,因为它可以使我们优雅地呈现注入了变量(例如$item)的HTML.

We use HEREDOC as it allows us to elegantly render HTML with variable(s) (e.g. $item) injected.

这篇关于PHP'json_decode'仅适用于第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆