-1

I have an API which is returning me response like this.

{
    "logs": [
        [
            "2018-05-22 00:10:16", 
            "Billed"
        ]
    ], 
    "package": "superpremium", 
    "subdate": "2018-05-08 14:18:18", 
    "submedium": "CALL"
}

I'm trying to change into array so It can be easily accessible for me. For example.

$response['subdate']; // echo 2018-05-08 14:18:18
$response['submedium']; // echo CALL
$response['package']; // echo superpremium

and the logs one should be like

$response['logs'];

logs must be an array so I can use foreach log to display all values of an array like 2018-05-22 00:10:16 or "Billed" etc

I have used below codes but returns empty screen.

json_decode($response, true);
json_decode($response);
Valar M
  • 51
  • 1
  • 1
  • 8
  • so what you've tried till now ? and I'm sure if you are looking into json to array php you will find something – Edwin May 25 '18 at 07:54
  • 1
    Possible duplicate of [json\_decode to array](https://stackoverflow.com/questions/5164404/json-decode-to-array) – Kaddath May 25 '18 at 07:55
  • I have used json_decode but returns empty – Valar M May 25 '18 at 07:55
  • @Kaddath doesnot work – Valar M May 25 '18 at 07:56
  • json seems incorrect because of `,` at last:-https://jsonlint.com/ – Alive to die - Anant May 25 '18 at 07:57
  • well, yes it does work. It just means you have something else going wrong in your code or your JSON. Is your variable `null`? it could indicate an error in the JSON parsing, and in this case using `json_last_error_msg()` you can have the eventual error – Kaddath May 25 '18 at 07:57
  • Yes it is returning `:null` when i do var_dump.. – Valar M May 25 '18 at 07:59
  • 1
    What code are you *actually* running? Because you haven't posted anything that would display any output - you're just running `json_decode` and not doing anything with the result, so a white screen is expected behaviour. – iainn May 25 '18 at 08:19

4 Answers4

1

I'm trying to change into array

Just cast it into one then:

$response = (array) json_decode($response);

After that, you can easily access $response['subdate'] etc.

CBroe
  • 91,630
  • 14
  • 92
  • 150
0

Use json_decode($your_response) method. Your json data will get converted to arrays.

Like:

$response = json_decode($api_response);

And then you can access the values as (By seeing your data, seems that you are getting object):

$subdate = $response->subdate; // echo 2018-05-08 14:18:18
$submedium = $response->submedium; // echo CALL
$package = $response->package;
0
$json_data = json_decode($YourJSONResponse, true);

Then these will be available:

$json_data["package"]
$json_data["subdate"]
$json_data["submedium"]
MatHatrik
  • 762
  • 1
  • 6
  • 16
-3

Please change date from:

"submedium": "CALL",

To:

"submedium": "CALL"

Remove char "," and used function json_decode it, and enjoy

  • I can't see how this answers the question. The example code doesn't have a trailing comma. – Lex May 25 '18 at 08:12
  • @Lex there was one. The OP edited without adding a notice to the question – Kaddath May 25 '18 at 08:18
  • @Kaddath I was wondering if that may be the case. In another comment above the OP mentions he put it there bu mistake. – Lex May 25 '18 at 08:20