How to convert json objects into a PHP array [duplicate] - php

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.

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

Trying to convert a variable in PHP to an array [duplicate]

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

Issues in json_encode [duplicate]

This question already has answers here:
Best way to create an empty object in JSON with PHP?
(4 answers)
Closed 6 years ago.
I have this array:
$array = [
"stored_fields" => ["id"],
"match_all" => []
];
I want:
{"stored_fields":["id"],"match_all":{}}
But, When I call:
echo json_encode($array);
I got:
{"stored_fields":["id"],"match_all":[]}
If I use:
echo json_encode($array, JSON_FORCE_OBJECT);
I got:
{"stored_fields":{"0":"id"},"match_all":{}}
What I can do?
There might be a better way, but you can just force an object:
$array = [
"stored_fields" => ["id"],
"match_all" => (object)[]
];
You could also use (object)null. Anything else will result in {"scalar":someting}. Another option is new stdClass.

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"));

Convert a multidimensional array to an XML object in PHP [duplicate]

This question already has answers here:
How to convert array to SimpleXML
(34 answers)
Closed 8 years ago.
I have a multidimensional array like this:
$array = array(
"hello" => "hola",
"another_array" => array(
"key" => "best key ever",
"another" => "yes, another key",
),
"coolarray" => array(
"bool" => true,
"string" => "this is a string!",
),
);
I want a class like this:
class MyClass {
public $array;
public function __construct($array) {
// something
$this->array_to_xml($array);
}
public function array_to_xml($array) {
// convert array to xml
}
Then I want to be able to do things like this:
$string = $this->array->coolarray->string;
How can I do that?
This gets asked a lot
Not sure why you mention XML, sounds like you just want an object.
See this answer for example:
https://stackoverflow.com/a/11854285/543455
$obj = json_decode(json_encode($array));

Categories