Using var_dump from an API call give me the output like this
How to put them into an array? thanks
You can use json_encode to convert it to a json. After which you can use json_decode with the second parameter set to true which will return an array instead of an StdObject.
If the api you're calling simply returns a json itself then you can go ahead and use json_decode directly with the second argument set to true to yield the same result.
There are other ways, but this is the simplest.
You can check out other questions, like this one or this one.
Related
When I print an object using print_r, is there any specific utility function in php to convert the output to an object?
There seems to be enough output to reconstruct the object, but I couldn't find the exact functions to accomplish this.
There is no automatic method to change print_r value back to an object. You are also missing important information like what type is stored at a key (string or number or ..)
You could use var_dump to get more detailed output that can be changed back to an object. But still this does not mean there is an automatic function for it.
Last you could use var_export ( http://php.net/manual/en/function.var-export.php ) to get valid PHP output you can use.
You could also use http://php.net/manual/en/function.serialize.php to pass objects around.
I am making a web app. In one part of it, I have JS send a string(in json format) to PHP.
The value php receives is:
{"date":"24-03-2014","Cars":["Cheap","Expensive"]}
Now, this is saved in a variable $meta. The problem I am facing is, as to how do I convert this string into an object and reference each individual entry separately.
I have tried json_decode and json_encode
and then I have referenced each variable using $meta.["date"] and $meta.date but nothing seams to work. I am getting just { as the output.
What's the correct way to do this?
$str = '{"date":"24-03-2014","Cars":["Cheap","Expensive"]}';
$obj = json_decode($str);
echo $obj->date;
// 24-03-2014
Usually a $my_obj = json_decode($_POST['jsonstring'], 1); (true supply means it'll be returned as an assoviative array) should be the way to go. If I were you I'd probably try a var_dump($my_obj); to see what actually comes through. If it doesn't work you'll want to make sure that you correctly submit a valid json string, e.g. JSON.stringify();
You should check out the PHP doc page for json_decode here.
By default, unless you pass true as the second parameter for json_decode, the function call will return an object, which you can access the members of by using:
$meta->date
The arrow operator will allow you to access object values, not the square brackets or a dot.
When I print an object using print_r, is there any specific utility function in php to convert the output to an object?
There seems to be enough output to reconstruct the object, but I couldn't find the exact functions to accomplish this.
There is no automatic method to change print_r value back to an object. You are also missing important information like what type is stored at a key (string or number or ..)
You could use var_dump to get more detailed output that can be changed back to an object. But still this does not mean there is an automatic function for it.
Last you could use var_export ( http://php.net/manual/en/function.var-export.php ) to get valid PHP output you can use.
You could also use http://php.net/manual/en/function.serialize.php to pass objects around.
I have a string, more specifically, this one:
a:16:{s:9:"pseudonym";O:16:"SimpleXMLElement":0:{}s:14:"parallel_title";O:16:"SimpleXMLElement":0:{}s:9:"title_var";O:16:"SimpleXMLElement":0:{}s:6:"series";O:16:"SimpleXMLElement":0:{}s:9:"vol_title";O:16:"SimpleXMLElement":0:{}s:9:"reference";O:16:"SimpleXMLElement":0:{}s:10:"bound_with";O:16:"SimpleXMLElement":0:{}s:15:"general_remarks";O:16:"SimpleXMLElement":0:{}s:6:"copies";O:16:"SimpleXMLElement":1:{i:0;s:1:"1";}s:11:"remarks_BPH";O:16:"SimpleXMLElement":0:{}s:3:"ICN";O:16:"SimpleXMLElement":1:{i:0;s:4:"neen";}s:10:"provenance";O:16:"SimpleXMLElement":0:{}s:7:"binding";O:16:"SimpleXMLElement":0:{}s:10:"size_hxwxd";O:16:"SimpleXMLElement":0:{}s:6:"BookID";O:16:"SimpleXMLElement":1:{i:0;s:4:"6271";}s:5:"repro";O:16:"SimpleXMLElement":0:{}}
Is it possible to parse this string somehow? I need to display the keys and values in a list. I tried to use json_decode but it doesn't return anything, even with the second parameter set to true:
json_decode($string,true);
It's not JSON, it's serialized PHP. Use unserialize().
It's serialize object
Read more on PHP website
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