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

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

问题描述

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

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

<tr class="css-xlbdcw"><td style="width: unset;最大宽度:未设置;填充:5px 20px;文本对齐:左;>><div class="vx_text-body-md";样式=颜色:RGB(44、46、47);填充顶部:7px;padding-bottom: 7px;"><div角色=按钮"><?php echo $obj2['data'][0]['activity_date'];?>

</td><td style="width: unset;最大宽度:未设置;填充:5px 20px;文本对齐:左;>><div data-testid="verticalList"><a tabindex=0"href=/activity/payment/87J62844XL761054H">

</td><td style="width: unset;最大宽度:未设置;填充:5px 20px;文本对齐:右;><div class="vx_text-body-md";样式=颜色:RGB(44、46、47);填充顶部:7px;padding-bottom: 7px;"><div role="button"><?php echo $obj2['data'][0]['activity_amount'];?></div>

</td></tr>

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

谢谢

解决方案

如果您希望自己灵活地呈现项目,可以使用过滤器数组 ($filter),如下所示:

';//打开一行foreach ($record as $key => $item) {if (in_array($key, $filter)) {//只显示过滤的项目回声<<<<div角色=按钮">$item

</td>赫里多克;}}回声'</tr>';//关闭行}

我们使用 HEREDOC 因为它允许我们优雅地使用注入的变量(例如 $item)渲染 HTML.

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>

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.

Thanks

解决方案

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
}

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

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

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆