How to convert php array to js array: 1. Convert $data into json encoding, and then use eval to convert the json format into js array; 2. Directly use the json encoded array in php.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
Convert php array to js array
Method 1
:In the controller:
public function index() { $sql=Db::name('shuju') ->field('name') ->select(); foreach($sql as $val) { $arr[]=$val['name']; } $this->assign('data',$arr); return $this->fetch('index'); }
Look at it What kind of array is $arr:
In the view:
First convert $data into json encoding, and then use eval to convert the json format into js array
Method 2:
Use directly in php json encoded array
[Recommended learning: PHP video tutorial]
The above is the detailed content of Convert php array to js array. For more information, please follow other related articles on the PHP Chinese website!