-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 :)

2
  • 1
    json_decode($str, true); Commented Jan 28, 2018 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. Commented Jan 28, 2018 at 20:11

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

PHP manual

Comments

0

This API response appears to be JSON.

Try doing:

$myArray = json_decode($api_response, true);

Where $api_response is the API response variable

Comments

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

4 Comments

You should choose an answer not post a copy... :P
Sorry... I didn't see anything that said to select an answer... Oops :-/
no biggie, it's a grayed out check-mark beside each submitted answers
ah nvm its been marked as duplicate...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.