Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysql生成一个json对象_Php_Mysql_Arrays_Json - Fatal编程技术网

Php mysql生成一个json对象

Php mysql生成一个json对象,php,mysql,arrays,json,Php,Mysql,Arrays,Json,这是我的代码: $query = "select ((recipients.maennlichDeutsch+recipients.maennlichAuslaender+recipients.weiblichDeutsch+recipients.weiblichAuslaender)/inhab.Einwohner) as Sozialhilfeempfaenger from recipients left join education on recipients.Bundesland = e

这是我的代码:

$query = "select ((recipients.maennlichDeutsch+recipients.maennlichAuslaender+recipients.weiblichDeutsch+recipients.weiblichAuslaender)/inhab.Einwohner) as Sozialhilfeempfaenger from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr where recipients.Bundesland in ('Thueringen') and education.Abschluss in ('Hauptschulabschluss')";
$result=mysqli_query($db, $query) or die('Error querying database.');
$data = array();
while($row =mysqli_fetch_assoc($result))
{
    $data[] = $row;
}
echo json_encode($data);
?>
这是我得到的json结果:

[    
    {
        "Sozialhilfeempfaenger": "0.0208"
    },    
    {
        "Sozialhilfeempfaenger": "0.0202"
    },    
    {
        "Sozialhilfeempfaenger": "0.0198"
    },    
    {
        "Sozialhilfeempfaenger": "0.0209"
    },    
    {
        "Sozialhilfeempfaenger": "0.0222"
    },    
    {
        "Sozialhilfeempfaenger": "0.0235"
    },    
    {
        "Sozialhilfeempfaenger": "0.0254"
    },    
    {
        "Sozialhilfeempfaenger": "0.0031"
    },    
    {
        "Sozialhilfeempfaenger": "0.0032"
    },    
    {
        "Sozialhilfeempfaenger": "0.0036"
    },    
    {
        "Sozialhilfeempfaenger": "0.0038"
    },    
    {
        "Sozialhilfeempfaenger": "0.0037"
    },    
    {
        "Sozialhilfeempfaenger": "0.0037"
    },    
    {
        "Sozialhilfeempfaenger": "0.0039"
    },    
    {
        "Sozialhilfeempfaenger": "0.0039"
    },    
    {
        "Sozialhilfeempfaenger": "0.0042"
    },    
    {
        "Sozialhilfeempfaenger": "0.0044"
    }
]
我该怎么做,以这种方式存储

{
  "sozialhilfeempfaenger": [0.0039, 0.0042...]

}

只需改变将数组推入容器的方式即可。只需直接选择索引,并将其推送到数组中

想法:


只需改变将数组推入容器的方式即可。只需直接选择索引,并将其推送到数组中

想法:


除了获取数据,您还尝试了什么?除了获取数据,您还尝试了什么?
$query = "select ((recipients.maennlichDeutsch+recipients.maennlichAuslaender+recipients.weiblichDeutsch+recipients.weiblichAuslaender)/inhab.Einwohner) as Sozialhilfeempfaenger from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr where recipients.Bundesland in ('Thueringen') and education.Abschluss in ('Hauptschulabschluss')";
$result=mysqli_query($db, $query) or die('Error querying database.');
$data = array();
while($row =mysqli_fetch_assoc($result))
{
    $data[] = $row['Sozialhilfeempfaenger'];
}
echo json_encode(array('Sozialhilfeempfaenger' => $data));
?>
$data['sozialhilfeempfaenger'] = array(); // initialize
while ($row = mysqli_fetch_assoc($result)) {
    $data['sozialhilfeempfaenger'][] = $row['Sozialhilfeempfaenger'];
                                      // ^ directly access the index
}
echo json_encode($data);