PHP 'json_decode' Chỉ hoạt động cho mặt hàng đầu tiên
Oct 28 2020
Tôi đang giải mã định dạng JSON, Điểm cuối API của tôi là https://api.reliableserver.host/api/recent_activities (Không xác thực)
Tôi đã quản lý để hiển thị các giá trị của mục đầu tiên như thế này:
<?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>
Điều này đang hoạt động cho mục đầu tiên, làm thế nào để tôi thực hiện nó đi qua từng mục bằng cách sử dụng foreach- Tôi đã thử nhưng không thể hoạt động.
Cảm ơn
Trả lời
1 u_mulder Oct 28 2020 at 07:10
Lặp lại $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
}
1 jibsteroos Oct 28 2020 at 07:30
Nếu bạn muốn cho phép mình linh hoạt trong việc hiển thị các mục, bạn có thể sử dụng một mảng bộ lọc ( $filter), như sau:
<?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
}
Chúng tôi sử dụng HEREDOC vì nó cho phép chúng tôi hiển thị HTML một cách trang nhã với (các) biến (ví dụ $item) được chèn vào.