PHP array Conversion JS array operation and Json_encode usage _php tutorial

Source: Internet
Author: User
For PHP, the personal feel capable of manipulating arrays and strings, is basically a primer, PHP itself has a lot of operations array and string functions, today in a function, you need to use JS dynamic creation of store information, this information is to be added from the background, want to go, read the database through PHP, Get the array, then convert the array into a matching demand JS array,
The format of the PHP array is:
Copy CodeThe code is as follows:
$newArray = Array (' Region ' = ' Beijing ', ' items ' = ' ', ' detail ' =>array (0=>array (' Store name ' = ' = ' flagship store ', ' url ' = > ' http://www '), 1=>array (' Store name ' = ' jjjj ', ' url ' = ' http://www.fdd ')),
Array (' region ' = ' Shanghai area ', ' items ' = ' one ', ' detail ' =>array (0=>array (' shop name ' = ' "flagship store ') ', ' url ' = ' + ' http://www. '), 1=>array (' Store name ' = ' jjjj ', ' url ' = ' http://www.fdd ')),
);

Here is a handy function: Json_encode ();
var jsarray = new Array ();
Jsarray = ;
Use Console.log (); You can see the structure of the Jsarray.

How do you construct such a conforming PHP array here? (I'm using the PHPCMS system)
Copy the Code code as follows:
$sql = ' Select Catid,catname,items from category where parentid=10 ';
$result = mysql_query ($sql);
while ($row = Mysql_fetch_array ($result)) {
$arrArea [' area '] = $row [' catname '];
$arrArea [' items '] = $row [' Items '];
unset ($arrArea [' detail ']);//This step is critical, or the information that is obtained will accumulate.
$sql 2 = ' Select Title,url from news where catid= '. $row [' catid '];
$fendian = mysql_query ($sql 2);
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: The variable is JSON-encoded, and the function can only accept UTF-8 encoded data.

The corresponding Json_decode, the second argument is true, returns the array, and executes the inverse process.

JSON can only be used with objects and arrays.

http://www.bkjia.com/PHPjc/824934.html www.bkjia.com true http://www.bkjia.com/PHPjc/824934.html techarticle for PHP, the personal feel capable of manipulating arrays and strings, is basically a primer, PHP itself has a lot of operations array and string functions, today in a function, need to use ...

  • Related Article

    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.