-1

I've got a variable that I've retrieved from an API call, and am trying to convert the output in to an array. Below is an example of the output:

{"id":"4335416","linkId":4335416,"title":"Website 1","slashtag":"1","destination":"http://website1.com","createdAt":"2017-09-20T12:10:56.000Z","updatedAt":"2017-12-01T13:59:35.000Z","status":"active","clicks":722,"lastClickDate":"2018-01-28T15:47:21.000Z","lastClickAt":"2018-01-28T15:47:21.000Z","isPublic":false,"shortUrl":"url.st/1","domainId":"abc123","domainName":"url.st","domain":{"id":"abc123","ref":"/domains/abc123","fullName":"url.st","active":true},"https":false,"favourite":false,"creator":{"id":"321cba","fullName":"user123","avatarUrl":"https://s.gravatar.com/avatar/"}},
{"id":"4335402","linkId":4335402,"title":"Website 2","slashtag":"2","destination":"http://website2.com","createdAt":"2017-09-20T12:09:29.000Z","updatedAt":"2017-12-01T13:58:56.000Z","status":"active","clicks":234,"lastClickDate":"2018-01-28T13:44:29.000Z","lastClickAt":"2018-01-28T13:44:29.000Z","isPublic":false,"shortUrl":"url.st/2","domainId":"abc123","domainName":"url.st","domain":{"id":"abc123","ref":"/domains/abc123","fullName":"url.st","active":true},"https":false,"favourite":false,"creator":{"id":"321cba","fullName":"user123","avatarUrl":"https://s.gravatar.com/avatar/"}},
{"id":"4335375","linkId":4335375,"title":"Website 3","slashtag":"3","destination":"http://website3.com","createdAt":"2017-09-20T12:07:23.000Z","updatedAt":"2017-12-20T18:43:17.000Z","status":"active","clicks":111,"lastClickDate":null,"lastClickAt":null,"isPublic":false,"shortUrl":"url.st/3","domainId":"abc123","domainName":"url.st","domain":{"id":"abc123","ref":"/domains/abc123","fullName":"url.st","active":true},"https":false,"favourite":false,"creator":{"id":"321cba","fullName":"user123","avatarUrl":"https://s.gravatar.com/avatar/"}}

I would like it to create an array within an array, as below:

$myArray = array( array( id => "4335416", 
                      linkId => 4335416,
                      title => "Website 1",
                      slashtag => "1",
                      destination => "http://website1.com",
                      ...
                    ),
               array( id => "4335402", 
                      linkId => 4335402,
                      title => "Website 2",
                      slashtag => "2",
                      destination => "http://website2.com",
                      ...
                    )
               array( id => "4335375", 
                      linkId => 4335375,
                      title => "Website 3",
                      slashtag => "3",
                      destination => "http://website3.com",
                      ...
                    )
             );

Completely stumped on this... Although my head has been spinning all day trying to do stuff :(

Thanks a lot :)

  • 1
    `json_decode($str, true);` – splash58 Jan 28 '18 at 20:03
  • In your paste of what the API returns, you are missing the open and close square braces. If this indeed is how the API returns the data, then just using json_decode alone won't work. You'll need to first add the square braces before using that function. – IncredibleHat Jan 28 '18 at 20:11

4 Answers4

1

You can convert it to an array with:

json_decode($myArray, true);

The second argument true will make it an associative array which has the named keys from your JSON data.

json_decode documentation

Daniel Twigg
  • 749
  • 4
  • 22
0

You are looking for json_decode('APIcallValue', true)

PHP manual

hexYeah
  • 1,040
  • 2
  • 14
  • 24
0

This API response appears to be JSON.

Try doing:

$myArray = json_decode($api_response, true);

Where $api_response is the API response variable

joshua miller
  • 1,686
  • 1
  • 13
  • 22
0

Thanks for that guys...

json_decode($api_response, true)

worked perfect. How simple when you know all the different commands! lmao. Should've just posted here first off... Saved me a couple of hours...

Thanks again