I have a PHP multidimensional array witch i converted to JSON using JSON_encode().
I am using drupal so for those not familiar with it, drupal array often have keys that looks like that :
$some_array['und']['0']['value']
So my JSON object ends up looking like:
some_array.und.0.value
Now the problem is that when use the above syntaxe to retreive the value i the following JS error in the FB console : "missing name after . operator"
Also this data is meant to be used with a Jquery template, so i alos tried accessing this data directly in my template with:
${field_city.und.0.value}
Witch unfortunately didn't work either..
How would i go to fix that issue ? Can i access somehow this proprety with JS? Or is there a way that i have JSON_encode function replace all 0 by let's say "zero"? Or even replacing '0' when parsing the JSON string?
Suggestions much appreicated.
Try accessing it with some_array.und["0"].value. '0' is not a valid name for a javascript object, which is why accessing it via the . notation is not working.
However, if you access it via the square brackets, you can access keys with any name at all.
As well as using the dot notation, you can use regular array notation to access JSON nodes:
some_array.und['0'].value
Related
I've one working REST API developed using Slim PHP framework.
It's working absolutely fine.
The only issue is when there is no error present i.e. an array $errors is empty it comes as an array in JSON response but when the array $errors contains any error like $errors['user_name'] then it comes as an object in JSON response.
Actually I want to return the array when error is present. How should I do this? Can someone please help me in this regard?
Thanks in advance.
When your $errors is not empty, pass it through json_encode and echo it.
It will give you JSON object in return,
then convert JSON object into JavaScript array. (see the following code.)
var o = {"0":"1","1":"2","2":"3","3":"4"}; // your response object here
var arr = Object.keys(o).map(function(k) { return o[k] });
console.log(arr);
Recently got a close issue with Symfony's JsonResponse::create()
Turns out arrays with index not starting at 0 will be encoded into objects, as well as arrays with "holes", and probably any array with at least one non-int key.
In other word, arrays with anything else than consecutive numeric keys starting from index 0 seem to be encoded as object.
I guess this is designed to avoid sending big empty arrays when you map a handful of datas with big indices like [14334, 839493, 246193], and is probably documented somewhere.
Learning if this is a Symfony of json_encode behavior from comments would be welcomed addition :)
Note : Even if you return an array, it seems necessary to wrap it in an object for GET request to prevent some XSSI and JSON-JavaScript Hijacking.
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.
I have an associative array after json_encode like this
{"1":"CourseA", "2":"CourseB"}
and it is stored in a php variable named $jsonObject.Now, I want to send this to a javascript function and use that array inside that function. The
function is invoked onclick like this:
link
The problem is: It shows an error:invalid id popup({....
Whats the reason and what should be the solution for that? btw, I am working in moodle and the above link is shown inside a moodle block and declared inside $this->content->text.
Html encode the JSON to escape special characters,
link
Hi this is the code i am following in my project.
$reports = $this->curl->simple_get('url');
echo is_array($reports)?"Is Array":"Not Array";exit;
It's giving Not Array as output.
I want to convert that into associative array.
The data you are getting is probably not an array, but a string containing an array structure, e.g. output by print_r(). This kind of data will not automatically be converted back into a PHP array.
To use this you can use a similar solution as brought out here:
Create variable from print_r output
it describes the print_r_reverse function that's brought out in php.net page.
how ever - this is kind of an ugly hack. I would suggest to change the page content and use json_encode() in the "url" page, and parse the content using json_decode()
Im running a javascript code which reads values from different XML files and it generates a multidimentional array based on those values. Now I need to pass this array to a PHP page. I tried different but it always pass the arrray as string not as an array.
Anyone has an idea :( ... and thank you very much
What Caleb said.
Use this and JSON encode your JS array to a string, send it over to PHP and use json_decode to decode it into a PHP array.
You need a JSON encoder/decoder to do that. Prototype has it implemented by default and with jQuery you can use jQuery-JSON
For example if you use Prototype as your JS library then you can convert your array into a string like that:
var example_multi_dim_arr = {"a":[1,2,3], "b": [4,5,6]};
var string_to_be_sent_to_server = Object.toJSON(example_multi_dim_arr);
And in the PHP side (assuming that the JSON string is passed to the script as a POST variable)
$multi_dim_arr = json_decode($_POST["variable_with_json"], true);
The last true field in json_decode indicates that the output should be in the form of an array ($multi_dim_arr["a"]) and not as an object ($multi_dim_arr->a).
NB! the function json_decode is not natively available in PHP 4, you should find a corresponding JSON class if you are using older versions of PHP. In PHP 5 everything should work fine.