首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >循环通过php中的json

循环通过php中的json
EN

Stack Overflow用户
提问于 2019-08-01 14:52:48
回答 1查看 78关注 0票数 1

我有一个从api得到的json:

代码语言:javascript
运行
复制
{
    "data": [{
        "BrandID": 1,
        "AccountTranID": "138483",
        "Datetime": {
            "date": "2019-07-31 21:26:15.513000",
            "timezone_type": 3,
            "timezone": "UTC"
        },
        "PartyID": 1819748,
        "UserID": "adel2X",
        "Currency": "USD",
        "ProductID": 3,
        "ProductCode": "EBZZ",
        "ProductTranID": "EBZZ-f90f07cdfeff820904c56696dd72e321",
        "GameInfoID": 698,
        "GameID": "80233",
        "GameTranID": "80238cc6c07f85144d8ade97760f2cbdbff",
        "TranType": "GAME_BET",
        "AmountReal": "-.010000000000000000",
        "AmountPlayableBonus": ".000000000000000000",
        "AmountReleasedBonus": ".000000000000000000",
        "BalanceReal": ".000000000000000000",
        "BalancePlayableBonus": ".000000000000000000",
        "BalanceReleasedBonus": ".000000000000000000",
        "RollbackTranID": null,
        "RollbackTranType": null
    }, {
        "BrandID": 1,
        "AccountTranID": "138484",
        "Datetime": {
            "date": "2019-07-31 21:26:16.037000",
            "timezone_type": 3,
            "timezone": "UTC"
        },
        "PartyID": 1819748,
        "UserID": "adel2X",
        "Currency": "USD",
        "ProductID": 3,
        "ProductCode": "GROOVY",
        "ProductTranID": "Bo-f90f07cdfeff820904c56696dd72e321re",
        "GameInfoID": 698,
        "GameID": "80233",
        "GameTranID": "80238cc6c07f85144d8ade97760f2cbdbff",
        "TranType": "GAME_WIN",
        "AmountReal": ".000000000000000000",
        "AmountPlayableBonus": ".000000000000000000",
        "AmountReleasedBonus": ".000000000000000000",
        "BalanceReal": ".000000000000000000",
        "BalancePlayableBonus": ".000000000000000000",
        "BalanceReleasedBonus": ".000000000000000000",
        "RollbackTranID": null,
        "RollbackTranType": null
    }]
}

然后我尝试使用下面的php代码遍历它:

代码语言:javascript
运行
复制
$data = file_get_contents('https://example.com/example');
$json = json_decode($data);
foreach($json->data as $row) {
    foreach($row as $key => $val) {
        echo $key . ': ' . $val;
        echo '<br>';
    }}

我使用json中的键'data‘来定义行,然后尝试提取值,但没有成功。我没有将解码值设置为"true“,因为我不想将其视为数组,这会是问题所在吗?有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2019-08-01 15:34:43

使用json_decode转换关联数组中的json。使用array_walk_recursive获取该数组的所有键和值。

代码语言:javascript
运行
复制
//convert the json in array using json_decode
$json_array = json_decode($json, true); //true is used for 'associative array'.
//using array_walk_recursive get all the key and value
array_walk_recursive($json_array, function($item, $key){
    echo $key.':'.$item.PHP_EOL; 
});

Demo

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57303300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档