I want to convert from this array
[{"tagid":"422"},{"tagid":"467"},{"tagid":"146"},{"tagid":"097"}]
to
["422","467","146","097"]
Is there a way to convert this without using loop in php, or the best possible method in php?
$json = '[{"tagid":"422"},{"tagid":"467"},{"tagid":"146"},{"tagid":"097"}]';
$arr = array_column(json_decode($json, true), 'tagid');
Related
I am converting object to array using
$data['payment_cheque'] = (array)$data['payment_cheque'];
What i Get is
"\u0000*\u0000items": ["2017-04-18","2017-06-28","2017-07-05"]
What I want is
["2017-04-18","2017-06-28","2017-07-05"]
Just try this
$data['payment_cheque'] =json_decode(json_encode($data['payment_cheque'] ,true),true);
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));
This question already has answers here:
json_encode/json_decode - returns stdClass instead of Array in PHP
(7 answers)
Closed 9 years ago.
I am using json_encode() to encode array into json format. but it returning object instead of array. I want to return an array not an object.
any body have any idea?
Basically json_decode() will return two types of data.
1) Object
2) Associative array
By default, json_decode() returns object type value.
But, if you want value as an array format you must use TRUE as a second argument in json_decode().
e.g,
$decoded_value = json_decode($json_encoded_value, TRUE);
use this code for decoding your encode json data
$encode = $your_json_encoded_data
json_decode($encode, TRUE);
actually json_encode function in php will return a json formatted string.
and if you want to parse json formatted string back in php
then you should use json_decode.
json_decode function will return data two types.
object & associtavie array.
json_decode(); return type object
json_decode(, TRUE); return type associtative array
You should use json_decode with TRUE param like following example:
$array = array(1,2,3);
$encode = json_encode($array);
$decode = json_decode($encode, TRUE);
Now $decode is array, not object.
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);
I'm trying to decode the following JSON using php json_decode function.
[{"total_count":17}]
I think the square brackets in output is preventing it. How do I get around that? I can't control the output because it is coming from Facebook FQL query:
https://api.facebook.com/method/fql.query?format=json&query=SELECT%20total_count%20FROM%20link_stat%20WHERE%20url=%22http://www.apple.com%22
PHP's json_decode returns an instance of stdClass by default.
For you, it's probably easier to deal with array. You can force PHP to return arrays, as a second parameter to json_decode:
$var = json_decode('[{"total_count":17}]', true);
After that, you can access the variable as $result[0]['total_count']
See this JS fiddle for an example of how to read it:
http://jsfiddle.net/8V4qP/1
It's basically the same code for PHP, except you need to pass true as your second argument to json_decode to tell php you want to use it as associative arrays instead of actual objects:
<?php
$result = json_decode('[{"total_count":17}]', true);
print $result[0]['total_count'];
?>
if you don't pass true, you would have to access it like this: $result[0]->total_count because it is an array containing an object, not an array containing an array.
$json = "[{\"total_count\":17}]";
$arr = Jason_decode($json);
foreach ($arr as $obj) {
echo $obj->total_count . "<br>";
}
Or use json_decode($json, true) if you want associative arrays instead of objects.