1

In one of my applications I get a JSON format data from php. Which is as similar as the following:

{
  "abc@mail.com": {
    "RequestSessions": [
      {
        "StartAt": "2020-03-29T05:18:37.973618Z",
        "RunTime": 292,
        "Captcha": 3,
        "TotalBidRequests": 40,
        "TotalSearchRequests": 2831,
        "TotalTradeRequests": 88
      }
    ],
    "ProfitSessions": [
      {
        "StartAt": "2020-03-29T13:56:11.8250985Z",
        "Profit": 42598,
        "EProfit": 3350,
        "Coins": 55674,
        "Ecoins": 28000
      }
]
},
"adc@mail.com": {
    "RequestSessions": [
      {
        "StartAt": "2020-03-29T05:18:37.973618Z",
        "RunTime": 292,
        "Captcha": 3,
        "TotalBidRequests": 40,
        "TotalSearchRequests": 2831,
        "TotalTradeRequests": 88
      }
    ],
    "ProfitSessions": [
      {
        "StartAt": "2020-03-29T13:56:11.8250985Z",
        "Profit": 42598,
        "EProfit": 3350,
        "Coins": 55674,
        "Ecoins": 28000
      }
]
}
  }

From this response, I just need to fetch email to process further operations in my application. But I can't identify how to decode this using PHP json_decode()

Can anyone tell me how can I decode this using PHP and fetch only email from this response?

Thanks

Md. Foysal
  • 103
  • 8
  • this question is a bit imprecise. Are you asking how to use json_decode ? if yes this has been discussed time and times over, here for example: https://stackoverflow.com/questions/5164404/json-decode-to-array If you are asking what to do once you have decoded it, it's different – Eagle1 Apr 02 '20 at 10:07
  • @Eagle1 well to be fair, the exact way you decode it (i.e. to object or to associative array) can have an influence on the subsequent solution - see answer below. – ADyson Apr 02 '20 at 10:13
  • sure, your answer is perfect. stack asked me to review this first question, I did my best trying to clarify it. – Eagle1 Apr 02 '20 at 10:50

1 Answers1

1

In that JSON, the email addresses are the keys of the object - i.e. the names of the properties. If you decode the JSON into an associative array you can get those values using PHP's array_keys function:

$data = json_decode($json, true);

foreach (array_keys($data) as $key)
{
    echo $key.PHP_EOL;
}

This outputs:

abc@mail.com
adc@mail.com

Demo: http://sandbox.onlinephpfunctions.com/code/d7890691a6baae9f9314c727a9ad0f6519214a10

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Please, can is it possible to get "ProfitSessions"=> Coins by this email. if yes, then how to? – Md. Foysal Apr 02 '20 at 10:14
  • Sure, you can get anything you want from the data. http://sandbox.onlinephpfunctions.com/code/82992ba79ce8e20f69af3bf171f223b00dda7676 . Please try and learn for yourself how to extract data from a PHP array, it's not too difficult once you learn the syntax and practice a bit. – ADyson Apr 02 '20 at 10:18
  • http://sandbox.onlinephpfunctions.com/code/82992ba79ce8e20f69af3bf171f223b00dda7676 from this code how to get only last coins value for every email? – Md. Foysal Apr 02 '20 at 10:48
  • [count](https://www.php.net/manual/en/function.count.php) the number of entries in the ProfitSessions array, and then reference the last index directly. – ADyson Apr 02 '20 at 10:51
  • or actually even more easily you can use the [end](https://www.php.net/manual/en/function.end.php) function. (I just googled "php return last array index" to get this - google is your friend if you type simple, direct searches. It helps if you are familiar with the correct terminology for data structures etc first. Then you can almost always find everything you need. So be sure to learn the correct names for the things you are using in your code. – ADyson Apr 02 '20 at 10:53