How to write array for json encode to look like this - php

I am trying to get a specific json response, but one array in the response is being passed as an object.
"countries":{"TW":8,"JP":5,"AU":6,"MX":12,"CL":4,"HK":2,"US":14,"AR":4,"ES":1,"BR":1,"MY":9,"IT":12,"DE":1,"GB":1,"PE":6,"TR":1,"KR":3,"IE":1,"CA":2,"FR":1,"VE":2,"IL":1,"PT":1,"NL":1,"PL":1}
But I need it to look like this:
"countries":[["Brazil", 40.5],["US", 30],["Canada", 19.5], ["England", 10]]
How do I build that array in PHP for the json_encode response looks like that?
Now I have:
$countries['US']=14;
$countries['CL']=4;
....
Then I add that array ($countries) to the $data array, which is the one json encoded
$data['countries'] = $countries;
Which gives the result I posted first. But I need that in that in the second format.
Anyone knows what am I missing?
Thanks!

$countries = Array();
$countries[] = Array('Brazil', 40.5);
...

Related

How to get value from JSON encode in PHP?

i have searched and searched but i don't know where i'm wrong getting a value from JSON encode, can you help me? Please don't kill me, i'm a newbie :)
My php:
<?php
$data = json_decode("document.json", true);
$getit = $data["likes"];
My JSON:
[{
"title" : "MYTITLE",
"image" : "MYIMAGE",
"likes" : 0
}]
EDIT
Thanks for the help this is now working!
json_decode expects an string, not an filename - so you have first get the contents of the given file. This could be easily achieved with file_get_contents.
Your current json structure contains an array(with currently only one element), which contains an object. So if you want the likes, you have to read the first element of the result array and of that(an associative array), the likes.
$data = json_decode(file_get_contents($filename), true);
$likes = $data[0]['likes'];
If you have more than one object in the given json file, you could loop over the data with foreach
foreach ($data as $element) {
echo "likes of {$element['title']}: {$element['likes']}\n";
}
For json_decode you have to pass JSON string, not a file name. Use file_get_contents to get JSON content and then decode it.
$data = json_decode(file_get_contents('document.json'), true);
$getit = $data[0]['likes'];
Your JSON is an array of objects, you first need to access the first element of your array :
$data[0]
Then you can access the key you want :
$getit = $data[0]['likes'];
Plus, as stated in a comment above, you need to pass a string to json encode, here is an code sample that should suit your needs :
<?php
$data = json_decode(file_get_contents('document.json'), true);
$getit = $data[0]["likes"];
Hope this helps.

Why does my JSON array turn into an object?

I am trying to unset a value from test_bots.json and save it back, but somehow the data format is being changed in the process.
test_bots.json contains this JSON array:
["John","Vladimir","Toni","Joshua","Jessica"]
My code looks like this:
$good = 'Toni';
$good_arr = file_get_contents('test_bots.json');
$good_arr = json_decode($good_arr);
if(in_array($good, $good_arr)){
$key = array_search($good, $good_arr);
unset($good_arr[$key]);
$good_arr2 = json_encode($good_arr);
file_put_contents('test_bots.json',$good_arr2);
}
The output that's saved is:
{"0":"John","1":"Vladimir","3":"Joshua","4":"Jessica"}
but I want the output to look like:
["John","Vladimir","Joshua","Jessica"]
I tried to unserialize the array before saving it, but it's not working.
Why is this happening?
In order for json_encode to convert a PHP array with numeric keys to a JSON array rather than a JSON object, the keys must be sequential. (See example #4 in the PHP manual for json_encode.)
You can accomplish this in your code by using array_values, which will reindex the array after you have removed one of the items.
$good_arr2 = json_encode(array_values($good_arr));

How do I "trim" this JSON with PHP?

I have a JSON that's strangely formatted ...but it's the way I receive it. Because the arrays inside are huge, simply copying and pasting it takes a long time, so I'm wondering if there's a PHP way to do it.
The way I get it is like this:
{"count":459,"results":[{"title":"Something ....}],"params":{"limit..},"type":"Listing","pagination":{"..":5}}
But I want to get only the "results" array, basically the part of [{"title":"Something ....}]. How would I do that?
Do
$arr = json_decode(your_json, true);
If you ned it as JSON again, do
json_encode($arr['results']);
You can get to that part as follows:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit":".."},"type":"Listing","pagination":{"..":5}}';
$obj = json_decode($json);
echo $obj->results[0]->title;
Outputs:
Something ....
You have to decode your json. Be sure that the json is valid!
Your decoded json returns an object of type stdClass instead of an array!
See below the tested code:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit": "foo"},"type":"Listing","pagination":{"foo":5}}';
$decoded = json_decode($json);
So you can access array results from the object $decoded:
print_r($decoded->results);
But, in the array, there are objects, thus:
echo $decoded->results[0]->title; // Something ....

What is the right way to access multidimensional json array in php?

I'm working on cookies that contain multidimensional JSON object array and I have bit of a problem accessing them. Kindly help me.
Here's a look at my problem:
I access the values from the html element and encode it before storing it into a variable,
var cookie_items = JSON.stringify({id: value, quantity: 1});
then I push this object into an array,
cookie_array.push(cookie_items);
finally, I push the cookie_array into another array with the associative name 'all',
final_cookie_array['all'] = cookie_array;
When I log final_cookie_array in the browser console, the output looks like this:
Object {all: Array[2]}
all: Array[2]
0: "{"id":"6","quantity":1}"
1: "{"id":"2","quantity":1}"
When I echo the cookie array in PHP, i.e., $_COOKIE['cookie_name']; it outputs the following:
[object Object]
But when I try echoing $_COOKIE['cookie_name']['all']; nothing gets displayed.
Can somebody please help me deal with this?
Thanks.
Try like example below
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$arr = json_decode($json,true);
var_dump($arr);
?>

How to get json element's property from Entire Json in PHP

My json value looks like this:
[{"Id":"1169"},{"Id":"1164"},{"Id":"1163"},{"Id":"1162"},{"Id":"1161"}]
Now i want to store only numeric values 1169,1164,1163,1162,1161 into different array.How can i do this?
Simple
$values = array_values(json_decode($json,true));
its not array its a json.
You can change from array to json and json to array.
$a = json_encode($yourArray);
AND
$b= json_decode($yourJson);

Categories