Questions about the JSON output format of PHP

Source: Internet
Author: User
 $select = $this->datas->query("select data01 from ds_meters_320114102 ORDER BY id desc limit 24");        $result = $select->result();        $datas = array();        foreach($result as $row){            $datas[] = $row;        }        //var_dump($datas);        echo json_encode($datas);

The above is the JSON that I query the database to derive, this JSON result after output is this kind of format:

[{"data01":"20.90"},{"data01":"20.90"},{"data01":"21.00"},{"data01":"20.90"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"20.90"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"20.90"},{"data01":"20.90"},{"data01":"20.90"},{"data01":"20.90"},{"data01":"20.90"}]

But this data is not what I want, I want to change this JSON into this format:

[20.90,20.90,21.00,20.90,21.00,21.00....]

How can I do that? Thank you

Reply content:

 $select = $this->datas->query("select data01 from ds_meters_320114102 ORDER BY id desc limit 24");        $result = $select->result();        $datas = array();        foreach($result as $row){            $datas[] = $row;        }        //var_dump($datas);        echo json_encode($datas);

The above is the JSON that I query the database to derive, this JSON result after output is this kind of format:

[{"data01":"20.90"},{"data01":"20.90"},{"data01":"21.00"},{"data01":"20.90"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"20.90"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"21.00"},{"data01":"20.90"},{"data01":"20.90"},{"data01":"20.90"},{"data01":"20.90"},{"data01":"20.90"}]

But this data is not what I want, I want to change this JSON into this format:

[20.90,20.90,21.00,20.90,21.00,21.00....]

How can I do that? Thank you

If it is an array of numeric indexes, then the return value of Json_encode () is the [] enclosed string, and if it is an array of string indexes, the return value of Json_encode () is the string enclosed in {}.
So, to solve this problem, you can try it: Echo Json_encode (Array_values ($datas))

Upstairs has been able to solve the problem of the landlord perfectly.

The solution, you should ask more questions why.

    • How could JSON be like this?
    • Why not use JSON instead of serialization (serialize)?

If you are with this question, Baidu can solve the problem.

JSON (JavaScript Object Notation) is a lightweight data Interchange Format . It makes it easy for people to read and write. It also facilitates the analysis and generation of machines. It is based on a subset of JavaScript programming Language, Standard ECMA-262 3rd edition-december 1999. JSON uses a text format that is completely independent of the program language , but also uses the C language (c, C + +, C #, Java, JavaScript, Perl, Python, and so on). These features make JSON an ideal data exchange language . Source

JSON is based on two types of structures:

A collection of name/value pairs (A collection of name/value pairs). In different programming languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or associative array (associative array).

an ordered list of values (an ordered list of values). In most languages, it is implemented as an array (array), vector (vectors), list, sequence (sequence). Source

It must have its own characteristics, will, it will always be. Learn a little bit. JSON China | JSON Chinese web

$datas [] = $row [DATA01];

You need to remove the key data01 in the datas array above.

PHPforeach($result as $row) {    $datas[] = $row['data01']}

This is OK, because the previous array has the string key data01 so only JS objects can be generated {"data01" : "20.90"} to represent the key-value relationship. You $datas can switch to an array of natural indexes.

Json_encode (Array_column ($datas, ' data01 '));

tips:php5.5 only supports Array_column, if your PHP version is lower than this, use the Foreach loop above.

  • 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.