gpt4 book ai didi

php - mysql 返回一个 json 对象

转载 作者:行者123 更新时间:2023-11-29 04:35:18 25 4
gpt4 key购买 nike

这是我的代码:

$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...]

}

最佳答案

只需将其更改为将数组插入容器内的方式即可。只需直接选择索引,然后将其压入数组即可。

想法:

$data['sozialhilfeempfaenger'] = array(); // initialize
while ($row = mysqli_fetch_assoc($result)) {
$data['sozialhilfeempfaenger'][] = $row['Sozialhilfeempfaenger'];
// ^ directly access the index
}
echo json_encode($data);

关于php - mysql 返回一个 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44180529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com