How to format strings in php

Source: Internet
Author: User
Php: how to format a string with the following database fields: namesum Zhang San 20 Li Si 30 Wang Wu 11 Ma zi 12 want to read data from the database and format it into the following format: varmyData = newArray (['Zhang San ', 20], ['Li Si', 30], ['Wang Wu', 11], ['ma Zi', 12]); how to format and str php format strings in C #
The database fields are as follows:
Name sum
Zhang San 20
Li Si 30
Wang Wu 11
Ma zi 12

To read data from the database and format it in the following format:
Var myData = new Array (['Zhang San', 20], ['Li Si', 30], ['Wang Wu', 11], ['ma Zi', 12]);

The format and stringbuilder functions in C # are easy to implement. I don't know if there are similar functions in PHP. please post the code.

------ Solution --------------------
PHP code
$ Data = array ('Zhang San', 20), array ('li Si', 30), array ('Wang Wu', 11), array ('ma Zi ', 12); $ str = ''; foreach ($ data as $ arr) {$ str. = ", ['{$ arr [0]}', {$ arr [1]}]";} // echo ltrim ($ str ,','); // ['Zhang San', 20], ['Li Si', 30], ['Wang Wu', 11], ['machin ', 12] $ myData = "var myData = new Array ($ str);"; // echo $ myData; // var myData = new Array (, ['Zhang San ', 20], ['Li Si', 30], ['Wang Wu', 11], ['ma Zi', 12]);
------ Solution --------------------
Just use json_encode ($ mydate); in json format.
------ Solution --------------------
PHP code
$ Data = array ('Zhang San', 20), array ('li Si', 30), array ('Wang Wu', 11), array ('ma Zi ', 12); echo json_encode ($ data );

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.