从流明错误中获取数据

时间:2017-12-15 02:01:41

标签: php json laravel lumen

我收到错误“未定义索引:从流明调用数据时的id_currency,请帮帮我

<?php

require '../vendor/autoload.php';
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'http://139.59.113.86/zeeposapi/public/api/currency', [
'api_token' => ['']
]);

$decodedPosts = json_decode($res->getBody(), true);
?>

<?php
foreach ($decodedPosts as $post)  { ?>
    <tr>
        <th scope="row">
        <?php echo $post['id_currency']; ?>
        </th>
    </tr>
<?php } ?>

1 个答案:

答案 0 :(得分:0)

错误通知&#34; id_currency&#34;在数组中不存在。

<强>输出

  

{       &#34;状态&#34;:1,       &#34;数据&#34;:[           {               &#34; id_currency&#34;:1           },           {               &#34; id_currency&#34;:2           }       ]       &#34; totalCount&#34;:2   }

您应该使用以下代码

<?php foreach($decodedPosts["data"] as $post) { ?>
    <tr>
        <th scope="row">
        <?php echo $post['id_currency']; ?>
        </th>
    </tr>
<?php } ?>