mysql导致一个json对象

时间:2017-05-25 12:25:01

标签: 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 = 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...]

}

2 个答案:

答案 0 :(得分:3)

只需按照在容器内推送数组的方式进行更改即可。只需直接选择索引,然后将其推入数组。

点子:

    signup: function (req, res) {

        // Attempt to signup a user using the provided parameters
        User.signup({
            name: req.param('name'),
            email: req.param('email'),
            password: req.param('password'),
            avatar: req.param('avatar'),
        }, function (err, user) {
            // res.negotiate() will determine if this is a validation error
            // or some kind of unexpected server error, then call `res.badRequest()`
            // or `res.serverError()` accordingly.
            if (err) return res.negotiate(err);

            // Go ahead and log this user in as well.
            // We do this by "remembering" the user in the session.
            // Subsequent requests from this user agent will have `req.session.me` set.
            req.session.me = user.id;
            req.session.name = user.name;


            // If this is not an HTML-wanting browser, e.g. AJAX/sockets/cURL/etc.,
            // send a 200 response letting the user agent know the signup was successful.
            if (req.wantsJSON) {
                return res.ok('Signup successful!');
            }

            // Otherwise if this is an HTML-wanting browser, redirect to /welcome.
            return res.redirect('/quiz');
        });
    }

答案 1 :(得分:1)

$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));
?>