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);
Related
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));
Im not sure what is happening, but if i do
json_encode()
On a single array, i get valid json, but if i do something like
$ar['key'] = "name";
$array[] = json_encode($ar);
$json = json_encode($array);
It will return invalid json like so:
["{"key":"name"}"]
The expected outcome is
[{"key":"name"}]
I have searched for hours trying to find what is going on.
Due to lack of desired outcome, I can only assume you are trying to get a multidimensional array.
The correct way to achieve this would be to build an array of arrays, and then json_encode the parent array.
$data = array();
$data['fruits'] = array('apple','banana','cherry');
$data['animals'] = array('dog', 'elephant');
$json = json_encode($data);
Following this code, $json will have the following value
{"fruits":["apple","banana","cherry"],"animals":["dog","elephant"]}
It could then be parsed properly by javascript using jQuery.parseJSON()
Just json_encode the entire array.
$ar['key'] = "name";
$json = json_encode($ar);
json_encode returns a string, and json encoding a string will return a string.
Also it's json_encode, not $json_encode
I have a string on my databas, that I'm trying to insert into a json object as an Array, not a string..:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
$jsonObject = array('numbers' => $arrayInString);
$json = json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
echo $json;
When I execute this.. my Json object is..
numbers: "[2,3,5,5,6]";
and not
numbers: [2,3,5,5,6];
Like I originally wanted.. can't get this to work, can anyone help?
Like was said you need to decode the passed data from the database and then build your array output. Something like this:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
// decode the JSON from the database as we build the array that will be converted back to json
$jsonObject = array('numbers' => json_decode($arrayInString));
echo json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
The slightly modified code above outputs:
{"numbers":[2,3,5,5,6]}
You need to json_decode your $arrayInString variable before you add it to the associative array.
I need an o/p like this ["12","13"] when I do the following json operation
I have a 2 varaiables and getting these values as post data
$a = $_POST['cas'];
$b = $_POST['casty'];
$final1 = json_encode($a);
$final2= json_encode($b);
$final_value = '['.$final1.','.$final2.']';
I am getting output as ["12","13"].I am doing correct way in php ? any other ways to get json object apart from this ?
Use an array for that like this:
$array = array($_POST['cas'], $_POST['casty']);
$final_value = json_encode($array);
Note: no need to create $a and $b.
By adding JSON_FORCE_OBJECT as a 2nd parmeter you'll get key => value data like a normal php array. JSON Arrays don't have keys, therefore most of the time JSON_FORCE_OBJECT is useful.
JSON Array ["data", "data2", "data3"]
JSON Object {0:"data", 1:"data2", 2:"data3"}
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);
...