1

i have this json i get from an api:

{
    "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
    }]
}

i then try to loop though it using this php code:

$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>';
    }}

I use the key 'data' in json to define the rows and then try to extract the values but no luck. I have not set the the decode value to "true" since i do not want to consider it an array, can this be the issue? Any suggestion?

R_life_R
  • 786
  • 6
  • 26
  • 2
    To parse json data to an array with `json_decode($json,true)` – LF00 Aug 01 '19 at 06:56
  • 1
    Some values of `$row` are objects of `stdClass`. You can't just `echo` objects. Use at least `print_r`. – u_mulder Aug 01 '19 at 06:58
  • If you try something like `echo $row->PartyID."
    ";` with just your outer loop, you should see how to display some of the basic details and work from there.
    – Nigel Ren Aug 01 '19 at 07:11

1 Answers1

1

convert the json in associative array using json_decode. using array_walk_recursive get all the key and value of that array.

//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

Shivendra Singh
  • 2,986
  • 1
  • 11
  • 11