For PHP, I personally feel that I am proficient in operating arrays and strings. I am basically getting started. PHP itself has many functions for operating arrays and strings. When working on a function today, I need to use JS to dynamically create store information. , this information needs to be added from the background. After much deliberation, I read the database through php, get the array, and then convert the array into a js array that meets the requirements.
The form of the php array is:
Copy code The code is as follows:
$newArray = array(array('Region'=>'Beijing Region','items'=>'10' ,'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'=>' 11','detail'=>array(0=>array('Store name'=>'Flagship store','url'=>'http://www.'),1=>array( 'Store name'=>'jjjj','url'=>'http://www.fdd'))),
);
A very convenient one to use here Function: json_encode();
var jsarray = new Array();
jsarray = ;
You can see it with console.log(); The structure of jsarray.
How to construct such a php array that meets the requirements here? (I am using the phpcms system)
Copy code The code is 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 very critical, otherwise the information will be lost Add up.
$sql2 = 'select title,url from news where catid='.$row['catid'];
$fendian = mysql_query($sql2);
while ($re=mysql_fetch_assoc($fendian )) {
$item['store name']=$re['title'];
$item['url']=$re['url'];
$arrArea['detail' ][] = $item;
}
$newwarr[]=$arrArea;
}
var_dump($newwarr);
json_encode: JSON the variable Encoding, this function can only accept UTF-8 encoded data.
The corresponding one is json_decode. The second parameter is true, returns an array, and performs the reverse process.
json can only use objects and arrays.
http://www.bkjia.com/PHPjc/824934.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824934.htmlTechArticleFor PHP, I personally feel that I am proficient in operating arrays and strings. I am basically getting started. PHP itself has many Functions that operate on arrays and strings. When doing a function today, you need to use...