php - 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 );
我可以打印出一长串看起来像数组的东西,但我无法将其中包含的数据作为单独的块访问。我错过了什么吗?
解决方案
我猜你在 foreach 中缺少这些键
$all_transactions_raw = file_get_contents("xxx");
$all_transactions = json_decode( $all_transactions_raw );
foreach ( $all_transaction['data']['transactions'] as $transaction ) {
echo $transaction;
}
推荐阅读
- airflow-scheduler - '{{ ds }}' 不增加
- reactjs - Reactrouter 匹配属性
- java - 添加到随机数
- python - 如何在 Python 中加入两个 groupby 数据框?
- terraform - gcp terraform - 如何将 pubsub_topic 连接到预算
- php - MySQL 查询执行两次
- kotlin - 类属性的运算符重载
- c# - 多个 CMD 命令 C#
- r - 标准化行中的名称并根据 R 中的相似行计算几何平均值
- xamarin.forms - Xamarin.Forms:离开主详细信息页面会导致之后的每个页面在标题中都有两个后退按钮