【问题标题】:PHP 'json_decode' Working for First Item OnlyPHP 'json_decode' 仅适用于第一项
【发布时间】:2021-02-10 12:41:47
【问题描述】:

我正在解码 JSON 格式,我的 API Endpoint 是 https://api.reliableserver.host/api/recent_activities (No Auth)

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

<?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 使其通过每个项目 - 我尝试但无法使其正常工作。

谢谢

【问题讨论】:

    标签: php json api loops foreach


    【解决方案1】:

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

    <?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。

    【讨论】:

      【解决方案2】:

      遍历$obj2['data']:

      $url2 = "https://api.reliableserver.host/api/recent_activities";
      $obj2 = json_decode(file_get_contents($url2), true);
      
      foreach ($obj2['data'] as $item) {?>
      <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 $item['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 $item['activity_title']; ?></a>
                  <div style="color: rgb(44, 46, 47); font-size: 11px;"><?php echo $item['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 $item['activity_amount']; ?></div>
              </div>
          </td>
      </tr>
      <?php
      }
      

      【讨论】:

      • 好极了,它的工作原理 - 9 分钟后接受答案,因为系统现在不允许我
      • 这个答案比另一个更准确
      猜你喜欢
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      相关资源
      最近更新 更多