Php array outputs such json

Source: Internet
Author: User
Php array outputs such json. how can I output such json?



{
Data = (
{
"Article_url" = "/disease/item/1 ";
"Create_time" = "11:38:58 ";
"Knowledge_id" = 1;
Title = "\ U6d4b \ U8bd5 \ U9057 \ U4f20 \ U75c5 \ U5e931 ";
},
{
"Article_url" = "/disease/item/3 ";
"Create_time" = "15:38:28 ";
"Knowledge_id" = 3;
Title = "\ U6211 \ U7684 \ U9057 \ U4f20 \ U77e5 \ U8bc6 ";
}
);
Errcode = 0;
Errdesc = "";
}

Data from the table
"Article_url" = "/disease/item/1 ";
"Create_time" = "11:38:58 ";
"Knowledge_id" = 1;
Title = "\ U6d4b \ U8bd5 \ U9057 \ U4f20 \ U75c5 \ U5e931 ";

Circular table reading program,

$ I = 0;
$ Listarr = array ();
While ($ db-> ROW ())
{
$ Listarr [$ I] ["article_url"] = $ ROW [article_url];
$ Listarr [$ I] ["create_time"] = $ ROW [create_time];
$ Listarr [$ I] ["knowledge_id"] = $ ROW [knowledge_id];
$ Listarr [$ I] ["titlee"] = $ ROW [title];
$ I ++;

}
How can I use this while program to output the top json?
Please help.


Reply to discussion (solution)

The above is assembled, and the code is incomplete. Errcode = 0;
Errdesc = ""; where did these two come from?

Your table array does not have this value errcode = 0; errdesc = "";
You can print the original array to see
Print_r ($ listarr );

This is the combination of arrays. This part of content


Data = (
{
"Article_url" = "/disease/item/1 ";
"Create_time" = "11:38:58 ";
"Knowledge_id" = 1;
Title = "\ U6d4b \ U8bd5 \ U9057 \ U4f20 \ U75c5 \ U5e931 ";
},
{
"Article_url" = "/disease/item/3 ";
"Create_time" = "15:38:28 ";
"Knowledge_id" = 3;
Title = "\ U6211 \ U7684 \ U9057 \ U4f20 \ U77e5 \ U8bc6 ";
}
);

Json? Yes ?? ? Method, I guess you mean

{    "data":[        {            "article_url":"/disease/item/1",            "create_time":"2014-08-11 11:38:58",            "knowledge_id":"1",            "title":"title1"        },        {            "article_url":"/disease/item/3",            "create_time":"2014-08-11 11:38:58",            "knowledge_id":"1",            "title":"title3"        }    ],    "errcode":"0",    "errdesc":""}


/** Json data formatting * @ param Mixed $ data * @ param String $ indent character. the default value is 4 spaces * @ return JSON */function jsonFormat ($ data, $ indent = null) {// recursively perform the urlencode operation on each element in the array to protect the Chinese character array_walk_recursive ($ data, 'jsonformatprotect '); // json encode $ data = json_encode ($ data); // urldecode $ data = urldecode ($ data) The urlencode content; // indent processing $ ret = ''; $ pos = 0; $ length = strlen ($ data); $ indent = isset ($ indent )? $ Indent: ''; $ newline =" \ n "; $ prevchar =''; $ outofquotes = true; for ($ I = 0; $ I <= $ length; $ I ++) {$ char = substr ($ data, $ I, 1); if ($ char = '"' & $ prevchar! = '\') {$ Outofquotes =! $ Outofquotes;} elseif ($ char = '}' | $ char = ']') & $ outofquotes) {$ ret. = $ newline; $ pos --; for ($ j = 0; $ j <$ pos; $ j ++) {$ ret. = $ indent; }}$ ret. = $ char; if ($ char = ',' | $ char = '{' | $ char = '[') & $ outofquotes) {$ ret. = $ newline; if ($ char = '{' | $ char = '[') {$ pos ++;} for ($ j = 0; $ j <$ pos; $ j ++) {$ ret. = $ indent ;}$ prevchar =$ char;} return $ ret;}/** urlencode the array element * @ param String $ val */Function jsonFormatProtect (& $ val) {if ($ val! = True & $ val! = False & $ val! = Null) {$ val = urlencode ($ val) ;}} header ('content-type: application/json; charset = utf8 '); $ result = array ('data' => array ('article _ url' => '/disease/item/1 ', 'create _ time' => '2017-08-11 11:38:58 ', 'knowledge _ id' => 1, 'title' => 'title1 '), array ('article _ url' => '/disease/item/3', 'create _ time' => '2017-08-11 11:38:58 ', 'Knowledge _ id' => 1, 'title' => 'title3'),), 'errcode' => 0, 'errdesc '=> ''); echo jsonFormat ($ result );

If no format is required ?, Yes
Echo jsonFormat ($ result );
Change?
Echo json_encode ($ result );
You can.

You can skip the above functions.

Thanks.

If no format is required ?, Yes
Echo jsonFormat ($ result );
Change?
Echo json_encode ($ result );
You can.

You can skip the above functions.

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.