PHP array conversion js array operation and json_encode application_PHP tutorial

WBOY
Release: 2016-07-14 10:10:34
Original
961 people have browsed it

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 php array format is: [php]
$newArray = array(array('Region'=>'Beijing Area','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'))),
);

$newArray = array(array('region'=>'Beijing area','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 function used here: json_encode();
var jsarray = new Array();
jsarray = ;
Use console.log(); to see the structure of jsarray. 

How to construct such a php array that meets the requirements here? (I am using phpcms system)


[php]
$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, otherwise the information obtained will be accumulated.
$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);

$sql= 'select catid,catname,items from category 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, otherwise the information obtained will be accumulated.
$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 encodes variables. This function can only accept UTF-8 encoded data.
Correspondingly, there is json_decode. The second parameter is true, returns an array, and performs the reverse process.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477488.htmlTechArticleFor PHP, I personally feel that I can operate arrays and strings proficiently. I am basically getting started. PHP itself has many Functions that operate on arrays and strings. When doing a function today, I need...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template