I sent POST request to my web-server with a few JSON-Params within the params there is an array. I receive for in the POST array-variable:
$ids = $_POST['id_arr']; // contains: [{\"id\":12},{\"id\":13}]
I don't know how to parse this to an array in PHP. I tried to solve it with json_decode, but it seems like the wrong way.
My desired result is this: $ids = array(12, 13);
How can I do this?
You need to tell the json_decode function to convert the objects to associative arrays. As shown in the json_decode documentation, this can be done by setting to true the second argument:
$ids = json_decode($_POST['id_arr'], true);
Related
Get all value from json using json_decode. Given name is Juliet"es this json. If using json_decode this array will be change null value. how do i get this json to array
$jsonobj = '{"Name":"Juliet"es","Maths":37,"English":43}';
if anyone having any idea please post answer.
Try $convertedJson = json_decode(addslashes($jsonobj));.
But has suggested by #Barmar and #CBroe , always generated JSON strings from array or object with json_encode().
I have the following json encoded object:
{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}
and I want to convert this into url string so I can use it with my REST API function for example like this one:
search?search=asdadd%2C+United+Kingdom&type=tutor
I have found many functions in javascript to do this but I haven't been able to find anything in PHP. What is the function in PHP to do this?
The following query string:
?username=my_username&email=my_email&password=12345678&confirm_password=12345678
.. will turn into:
{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}
If you use json_enconde.
To reverse the process, you need to use json_decode as well as http_build_query.
First, turn the JSON into an associative array with json_decode:
$json = '{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}';
$associativeArray = json_decode($json, true);
Now use http_build_query with the new associative array we've built:
$queryString = http_build_query($associativeArray);
Result: username=my_username&email=my_email&password=12345678&confirm_password=12345678.
I have a JSON object that I'm POST'ING to PHP from ExtJS interface. I get the object from
$json = $_POST["newUserInfo"];
The object will contain 3 arrays, which I can see if I do
var_dump(json_decode($json));
I need to take each array and build SQL queries from them. My first obstacle is getting the arrays out of the object, though this may be unnecessary. Here is the code block I'm working from:
/*Variable passed in from the ExtJS interface as JSON object*/
$json = $_POST["newUserInfo"];
//$json = '{"USER":{"ID":"","FULL_USER_NAME":"Some Guy","ENTERPRISE_USER_NAME":"guyso01","USER_EMAIL":"Some.Guy#Email.com","USER_PHONE":"123-456-7890"},"PERMISSIONS":{"ID":"","USER_ID":"","IS_ADMIN":"true"},"SETTINGS":{"ID":"","USERS_ID":"","BACKGROUND":"default"}}';
//Test to view the decoded output
//var_dump(json_decode($json));
//Decode the $json variable
$jsonDecoded = json_decode($json,true);
//Create arrays for each table from the $jsonDecoded object
$user_info = array($jsonDecoded['USER']);
$permissions_info = array($jsonDecoded['PERMISSIONS']);
$settings_info = array($jsonDecoded['SETTINGS']);
I'm not creating the arrays correctly. I've also tried
$user_info = $jsonDecoded->USER;
and that doesn't work either. I'm sure I'm missing something easy here. Again, this may be unnecessary as I can probably access them directly. I need to build the query by looping through the array and appending each key to a string and each value to a string. So I'd end up with something like
$query = "INSERT INTO USERS ($keyString) VALUES ($valueString);
Then I'd repeat the same process for PERMISSIONS and SETTINGS arrays. This is probably simple but I'm stuck here.
If you are using json_decode($json,true); - true means returning the js objects results as associative arrays - then all you have to do is $user_info = $jsonDecoded['USER']; without the array() cast cause that is what json_decode do for you.
If you would choose to omit the second boolean parameter then you will get an stdClass which $jsonDecoded->USER; would work for you
I have a json record like blow
[{"low":null,"high":10,"type":2,"value":0},{"low":10,"high":60,"type":1,"value":10},{"low":60,"high":null,"type":2,"value":11}]
I would like to create array using this. i tried json_decode its not help i just want create a associative array from this json
any help
thank you
Make sure you're passing true to the second parameter in json_decode(), which specifies that you want an associative array returned.
$arr = json_decode($string, true);
Parameters
assoc
When TRUE, returned objects will be converted into associative arrays.
see this page : http://php.net/manual/en/function.json-decode.php
use json_decode with this format : json_decode($value,true);
your code : http://pastebin.com/4vdVHjQN
I want to fill a 2D array in php from a JSON object in javascript at client. Can anybody help me to do this functionality?
From the top of my head, without knowing anything about your code
you could use json_decode
use it like this:
$array = json_decode($_POST['data'], true);
#check the second parameter set to true, otherwise you will get a stdclass.
json_decode()
http://php.net/manual/en/function.json-decode.php