Reputation: 15
this is my code:
$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);
?>
and this is the json result i get :
[
{
"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"
}
]
what do i have to do, to store it this way?
{
"sozialhilfeempfaenger": [0.0039, 0.0042...]
}
Upvotes: 1
Views: 39
Reputation: 41885
Just change it the way you push the array inside the container. Just select the index directly, and push it inside the array.
Idea:
$data['sozialhilfeempfaenger'] = array(); // initialize
while ($row = mysqli_fetch_assoc($result)) {
$data['sozialhilfeempfaenger'][] = $row['Sozialhilfeempfaenger'];
// ^ directly access the index
}
echo json_encode($data);
Upvotes: 3
Reputation: 2714
$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));
?>
Upvotes: 1