首页 > 解决方案 > php 不能使用 API 数据

问题描述

我发送一个请求并取回 JSON,所以我将该 JSON 转换为一个数组,但我不能在我的 php 中使用该数组:

$all_transactions_raw = file_get_contents("xxx");

$all_transactions = json_decode( $all_transactions_raw );

 foreach ( $all_transaction as $transaction ) {
    echo $transaction;
  }

超长输出的第一部分:

Array ( 
    [data] => Array ( 
        [transactions] => Array ( 
            [0] => Array ( 
                [id] => t1rtn-gsjkadfv 
                [date] => 2017-11-01 
                [amount] => -8847
                [memo] => 
                [cleared] => reconciled 
                [approved] => 1 
                [flag_color] => 

为什么上面的代码不打印 api 请求给出的交易列表?

当我使用时,print_r( $all_transactions );我可以打印出一长串看起来像数组的东西,但我无法将其中包含的数据作为单独的块访问。我错过了什么吗?

标签: phpjsonapi

解决方案


我猜你在 foreach 中缺少这些键

$all_transactions_raw = file_get_contents("xxx");

$all_transactions = json_decode( $all_transactions_raw );

 foreach ( $all_transaction['data']['transactions'] as $transaction ) {
    echo $transaction;
  }

推荐阅读