Convert json to array

Source: Internet
Author: User
Tags convert json to array
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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.