Trying to convert a variable in PHP to an array [duplicate] - php
This question already has answers here:
How to convert JSON string to array
(17 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
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 :)
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
You are looking for json_decode('APIcallValue', true)
PHP manual
This API response appears to be JSON.
Try doing:
$myArray = json_decode($api_response, true);
Where $api_response is the API response variable
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
Related
How to convert PHP multidimensional associative array to API http query in the format the API specifies? [duplicate]
This question already has answers here: How to save PHP array in database by converting array into JSON? (3 answers) Format array output (1 answer) Convert a series of PHP variables to a JSON object [duplicate] (3 answers) Converting MySQL result array to JSON [duplicate] (2 answers) Convert flat array to a delimited string to be saved in the database (13 answers) Closed 9 months ago. I have the following array $folder_data = array( "title" => "Testing API Creation", "description" => "Testing the Wrike API by creating this folder.", "project" => array( "status" => "OnHold", "startDate" => "2022-05-19", "endDate" => "2022-06-19", "contractType" => "Billable", "budget" => 100 ) ); When I run it through http_build_query(), it gives me this (urldecode() used for clarity): title=Testing API Creation&description=Testing the Wrike API by creating this folder.&project[status]=OnHold&project[startDate]=2022-05-19&project[endDate]=2022-06-19&project[contractType]=Billable&project[budget]=100 The API throws an error saying that project[status] is an invalid parameter The API docs give this within the curl example. You can see that the data for "project" is grouped: title=Test folder&description=Test description&project={"ownerIds":["KUFK5PMF"],"startDate":"2021-10-19","endDate":"2021-10-26","contractType":"Billable","budget":100} They're nesting the query into objects, I guess? How would I go about doing that with my PHP array? I tried a recursive function someone had done, but it didn't give me what it's looking for either. Any help is appreciated! Thanks!
The project parameter is JSON in their example, so use json_encode() to create that. $folder_data = array( "title" => "Testing API Creation", "description" => "Testing the Wrike API by creating this folder.", "project" => json_encode(array( "status" => "OnHold", "startDate" => "2022-05-19", "endDate" => "2022-06-19", "contractType" => "Billable", "budget" => 100 )) );
Pushing array into multidimensional array (php) [duplicate]
This question already has an answer here: array_push won't give an array, prints out integer value (1 answer) Closed 4 years ago. I've attempted many different ways to push an array into a multidimensional array, including array_push(), $array['index'] = $toPush but I keep being met with quite unexpected results. I have used both var_dump() and print_r() as detailed below in an attempt to debug, but cannot work out the issue. My reasoning behind is to run a while loop to pull game id's and game names and store these in an assoc. array, and then push them into my main array. $games_array = array ( "games" => array ( array("id"=>"1", "game"=>"first game"); array("id"=>"2", "game"=>"second game"); ) ); // a while loop would run here and update $game_to_add; $game_to_add = array("id"=>"$game['id']", "game"=>"$game['title']"); $games_array = array_push($games_array['games'], $game_to_add); In this example, the while() would update the ID and the Game inside of $game_to_add But, whenever I attempt this it simply overwrites the array and outputs an integer ( example: int(3) ) I don't understand what the problem is, any explination would be appreciated as I cannot find a question specifically for this. My actual test code: $games_array = array( "games" => array( array("id" => "1", "name" => "Star feathers"), array("id" => "2", "name" => "chung fu") ) ); $another_game = array("id" => "3", "name" => "some kunt"); $games_array = array_push($games_array["games"], array("id" => "3", "name" =>"some game")); var_dump($games_array);
You're assigning the return value of array_push to the games array. The return value of array_push is the amount of elements after pushing. Just use it as array_push($array, $newElement); (Without assignment) If you're only pushing one element at he time, $array[] = $newElement is preferred to prevent overhead of the function call of array_push
how to use array_push() [duplicate]
This question already has answers here: Add values to an associative array in PHP (2 answers) Closed 7 years ago. I want to push new data in array which each value of them. $array = array("menu1" => "101", "menu2" => "201"); array_push($array, "menu3" => "301"); But I got an error syntax. And if I use like this : $array = array("menu1" => "101", "menu2" => "201"); array_push($array, "menu3", "301"); result is : Array ( [menu1]=>101 [menu2]=>201 [0]=>menu3 [1]=>301 ) My hope the result is : Array ( [menu1]=>101 [menu2]=>201 [menu3]=>301 ) I want push new [menu3]=>'301' but I dont know how. Please help me, the answer will be appreciate
You can use $array["menu3"] = "301" as for array_push array_push() treats array as a stack, and pushes the passed variables onto the end of array so for associative arrays is a no match another suitable function for what you want but it requires an array argument is array_merge $result = array_merge(array("one" => "1"), array("two" => "2"));
Why is this PHP array not the same? [duplicate]
This question already has answers here: How can I access an array/object? (6 answers) Closed 4 months ago. I'm not understanding why the array: <? $data = array( 'items[0][type]' => 'screenprint'); ?> Is not the same as <? echo $data['items'][0]['type']; ?> I'm trying to add to the array but can't seem to figure out how?
array( 'items[0][type]' => 'screenprint') This is an array which has one key which is named "items[0][type]" which has one value. That's not the same as an array which has a key items which has a key 0 which has a key type. PHP doesn't care that the key kinda looks like PHP syntax, it's just one string. What you want is: $data = array('items' => array(0 => array('type' => 'screenprint'))); I hope it's obvious that that's a very different data structure.
It should be: $data = [ 'items' => [['type' => 'screenprint']] ]; echo $data['items'][0]['type'];
How to convert json objects into a PHP array [duplicate]
This question already has answers here: How to convert JSON string to array (17 answers) Closed 8 years ago. I want to be able to convert my JSON object into an array is there a built in function in php that allows me to do this? Here is my json object: in_userid: "38" in_email: "user#email.com" in_firstname: "John" in_lastname: "Mason" in_mobilenumber: "00000000000" in_password: "pass12345" in_phonenumber: "0000000" and I want it to look something like this: Array( in_userid => "38", in_email => "user#email.com", in_firstname => "John", in_lastname => "Mason", in_mobilenumber => "00000000000", in_password => "pass12345", in_phonenumber => "0000000", ); How will I do this?
use json_decode(). It takes a JSON encoded string and converts it into a PHP variable. See the json_decode function in the docs.