繁体   English   中英

PHP中来自MySQL数据库的Javascript数组

[英]Javascript array from mysql database in php

 var chartData = [{
     "country": "Czech Republic",
     "litres": 156.9
 }, {
     "country": "Ireland",
     "litres": 131.1
 }, {
     "country": "Germany",
     "litres": 115.8
 }, {
     "country": "Australia",
     "litres": 109.9
 }, {
     "country": "Austria",
     "litres": 108.3
 }, {
     "country": "UK",
     "litres": 65
 }, {
     "country": "Belgium",
     "litres": 50
 }];

我想在php中使用mysql数据库创建此数组,我该怎么做?

您可以从数据库以数组格式获取记录,然后它将转换为json对象。 试试下面的代码

// fetch the record form mysql table using mysqli_fetch_assoc() and store it in $chartData
    // then use json_decode() function used to convert the array into object format
    $chartData[] = array("country"=> "Czech Republic", "litres"=> 156.9);
    $chartData[] = array("country"=> "Czech Republic", "litres"=> 156.9);
    $chartData[] = array("country"=> "Czech Republic", "litres"=> 156.9);
    $chartData[] = array("country"=> "Czech Republic", "litres"=> 156.9);
    $chartData[] = array("country"=> "Czech Republic", "litres"=> 156.9);
    $chartData[] = array("country"=> "Czech Republic", "litres"=> 156.9);
    echo '<pre>';
    var_dump(json_encode($chartData));

我认为这将对您有所帮助.. :-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM