Json to array {"data": [{"count": "0" },{ "count": "0" },{ "count": "0 "}, {"count": "0" },{ "count": "4" },{ "count": "0"}], "result": 0}
How to convert the json format
{"Data": [0, 0, 0, 40], "result": 0}
Reply to discussion (solution)
Is converted to this: {"data": [,], "result": 0}
Well, why? Is the object (stdClass) displayed when json_decode is used)
This conversion can be used.
Function object_array ($ array ){
If (is_object ($ array )){
$ Array = (array) $ array;
}
If (is_array ($ array )){
Foreach ($ array as $ key => $ value ){
$ Array [$ key] = object_array ($ value );
}
}
Return $ array;
}
Found online
Excuse me, what is this thing? Object (stdClass), why? Thank you!
Or:
The result set returned from the SQL statement is a field: count field. There are several rows in the number of rows. In this example, there are six rows. To output this structure: {"data": [,], "result": 0}
$a = '{"data":[{"count":"0"},{"count":"0"},{"count":"0"},{"count":"0"},{"count":"4"},{"count":"0"}],"result":0}';$arr = json_decode($a, true);foreach($arr as $k=>$v){if($k == 'data'){foreach($v as $vv){$res[$k][] = $vv['count'];}}else{$res[$k] = $v;}}echo json_encode($res);
{"Data": ["0", "0", "0", "0", "4", "0"], "result": 0}
How can I change the string "0" to an integer 0?
{"Data": [0, 0, 0, 4, 0], "result": 0}
$s = '{"data":[{"count":"0"},{"count":"0"},{"count":"0"},{"count":"0"},{"count":"4"},{"count":"0"}],"result":0}';$a = json_decode($s, true);$a['data'] = array_map('current', $a['data']);echo json_encode($a);
{"data":["0","0","0","0","4","0"],"result":0}
If you don't want the numeric value to be enclosed in quotation marks
array_walk_recursive($a, function(&$v) {$v= intval($v);});echo json_encode($a);
{"data":[0,0,0,0,4,0],"result":0}