PHP 'json_decode' funktioniert nur für das erste Element
Oct 28 2020
Ich decodiere das JSON-Format, mein API-Endpunkt ist https://api.reliableserver.host/api/recent_activities (Keine Authentifizierung)
Ich habe es bereits geschafft, die Werte des ersten Elements wie folgt anzuzeigen:
<?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>
Dies funktioniert für das erste Element. Wie bringe ich es dazu, jedes Element mit zu durchlaufen? foreachIch habe es versucht, konnte es aber nicht zum Laufen bringen.
Vielen Dank
Antworten
1 u_mulder Oct 28 2020 at 07:10
Iterieren über $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
Wenn Sie sich Flexibilität beim Rendern von Elementen erlauben möchten, können Sie ein Filterarray ( $filter) wie folgt verwenden:
<?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
}
Wir verwenden HEREDOC, da es uns ermöglicht, HTML mit $iteminjizierten Variablen (z . B. ) elegant zu rendern .