Php array conversion js array operation and json_encode application

Source: Internet
Author: User
For php, I personally feel that I am familiar with array and string operations. php itself has a lot of functions for operating arrays and strings. today, when I create a function, the store information needs to be dynamically created using Js. This information needs to be added from the backend. if you want to use php to read the data... "/> <scripttype =" text/javascript "src =" http: // www.

For php, I personally feel familiar with array and string operations. it is basically an entry. php itself has a lot of functions for operating arrays and strings,
When creating a function today, you need to use Js to dynamically create store information. This information needs to be added from the background. if you want to use php to read the database
To obtain the array, and then convert the array into a js array that meets the requirements,
Php array format: [php]
$ NewArray = array ('region' => 'Beijing', 'items '=> '10 ', 'detail' => array (0 => array ('shop name' => 'flagship Store ', 'URL' => 'http: // www. '), 1 => array ('shop name' => 'jjjj', 'URL' => 'http: // www. fdd '))),
Array ('region' => 'Shanghai', 'items '=> '11 ', 'detail' => array (0 => array ('shop name' => 'flagship Store ', 'URL' => 'http: // www. '), 1 => array ('shop name' => 'jjjj', 'URL' => 'http: // www. fdd '))),
);

$ NewArray = array ('region' => 'Beijing', 'items '=> '10 ', 'detail' => array (0 => array ('shop name' => 'flagship Store ', 'URL' => 'http: // www. '), 1 => array ('shop name' => 'jjjj', 'URL' => 'http: // www. fdd '))),
Array ('region' => 'Shanghai', 'items '=> '11 ', 'detail' => array (0 => array ('shop name' => 'flagship Store ', 'URL' => 'http: // www. '), 1 => array ('shop name' => 'jjjj', 'URL' => 'http: // www. fdd '))),
);
A very convenient function is used here: json_encode ();
Var jsarray = new Array ();
Jsarray = ;
Use console. log (); to view the jsarray structure.

How to construct such a qualified php array here? (I am using a phpcms system)


[Php]
$ SQL = 'SELECT catid, catname, items from category where parentid = 10 ';
$ Result = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ result )){
$ ArrArea ['region'] = $ row ['catname'];
$ ArrArea ['items '] = $ row ['items'];
Unset ($ arrArea ['detail']); // This step is critical. Otherwise, the information will be accumulated.
$ Sql2 = 'SELECT title, url from news where catid = '. $ row ['catid'];
$ Fendian = mysql_query ($ sql2 );
While ($ re = mysql_fetch_assoc ($ fendian )){
$ Item ['shop name'] = $ re ['title'];
$ Item ['URL'] = $ re ['URL'];
$ ArrArea ['detail'] [] = $ item;
}
$ Newwarr [] = $ arrArea;
}
Var_dump ($ newwarr );

$ SQL = 'SELECT catid, catname, items from category where parentid = 10 ';
$ Result = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ result )){
$ ArrArea ['region'] = $ row ['catname'];
$ ArrArea ['items '] = $ row ['items'];
Unset ($ arrArea ['detail']); // This step is critical. Otherwise, the information will be accumulated.
$ Sql2 = 'SELECT title, url from news where catid = '. $ row ['catid'];
$ Fendian = mysql_query ($ sql2 );
While ($ re = mysql_fetch_assoc ($ fendian )){
$ Item ['shop name'] = $ re ['title'];
$ Item ['URL'] = $ re ['URL'];
$ ArrArea ['detail'] [] = $ item;
}
$ Newwarr [] = $ arrArea;
}
Var_dump ($ newwarr );
Json_encode: JSON encoding of the variable, which can only accept data encoded by the UTF-8.
Corresponding to json_decode, the second parameter is true, returns an array, and executes the inverse process.

 

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.