Question about the json output format of php

Source: Internet
Author: User
{Code ...} the above is the json obtained from the database query. The json result is output in the following format: {code ...} however, this data is not what I want. I want to convert this json into this format: {code ...} how can this problem be achieved? Thank you.
 $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 output from the database query. The json output format is as follows:

[{"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"}]

However, this data is not what I want. I want to convert this json into this format:

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

How can this problem be achieved? 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 output from the database query. The json output format is as follows:

[{"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"}]

However, this data is not what I want. I want to convert this json into this format:

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

How can this problem be achieved? Thank you.

If it is an array of numeric indexes, then the return value of json_encode () is a string enclosed by []. If it is an array of string indexes, then json_encode () the returned value is a string enclosed.
To solve this problem, try echo json_encode (array_values ($ datas ))

The upstairs can perfectly solve the problem of the landlord.

In addition, you should ask why.

  • How does JSON look like this?
  • Why do I have to use JSON instead of serialize )?

If you have such questions, Baidu can solve the problem.

JSON (JavaScript Object Notation) isLightweight Data Exchange Format. It makes it easy for people to read and write. It also facilitates machine parsing and generation. It is a subset of JavaScript-based Programming Language, Standard ECMA-262 3rd Edition-December 1999.JSON uses a text format completely independent of the Programming LanguageBut it also uses C-like habits (including C, C ++, C #, Java, JavaScript, Perl, and Python ). These features make JSONIdeal Data Exchange Language. Source

JSON is based on two structures:

Set of "name/value" Pairs(A collection of name/value pairs ). In different programming languages, it is understood as an object, record, struct, dictionary, and hash table ), keyed list or associative array ).

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

It must have its own characteristics. If yes, it will always happen. Let's take a look at JSON China | JSON Chinese Network

$ Datas [] = $ row [data01];

You need to remove the key data01 from the preceding datas array.

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

In this case, because the previous array contains the string key data01, only Js objects can be generated.{"data01" : "20.90"}To represent the key-value relationship. You put$datasReplace it with a natural index array.

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

Tips: php5.5 only supports array_column. If your php version is earlier than this, use the preceding foreach loop.

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.