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 ...