将数组推入数组然后编码 - PHP

时间:2017-06-30 15:37:22

标签: php mysql arrays

我试图在一个PHP中合并一些mySQL查询,然后将它们全部推送到一个JSON对象中。

所以,我从第一个查询开始......就像这样:

$data=[];

$response = $stmt->fetchAll(PDO::FETCH_ASSOC);
$final_count = count($response);
$data['count'] = $final_count;

如果我那么echo json_encode($data);我得到一个格式很好的对象,如:{"count":61}

然后我有第二个查询,我将结果放在一个循环中,如下所示:

$response = $stmt->fetchAll(PDO::FETCH_ASSOC);
$items = array();
foreach ($response as &$value) {
    $items[] = $value['date_added'];
}
echo json_encode($items);

我得到了很好的日期:

["2017-06-24 00:08:58","2017-06-26 15:01:48","2017-06-27 15:01:48","2017-06-28 23:19:41","2017-06-29 01:38:07","2017-06-30 00:08:58"]

问题是,如何将这一切重新组合在一起:

{
    "count": 61,
    "dates": [
        "2017-06-24 00:08:58",
        "2017-06-26 15:01:48",
        "2017-06-27 15:01:48",
        "2017-06-28 23:19:41",
        "2017-06-29 01:38:07",
        "2017-06-30 00:08:58"
    ]
}

3 个答案:

答案 0 :(得分:4)

    2017-06-30 17:38:37.124333+0200 XXXXX[83954:2132661] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x61000028b680 H:|-(30)-[UIView:0x7f8dab565720]   (active, names: '|':UIView:0x7f8dab566620 )>",
    "<NSLayoutConstraint:0x61000028b6d0 UIView:0x7f8dab565720.trailing == UIView:0x7f8dab566620.trailing - 30   (active)>",
    "<NSLayoutConstraint:0x61000028b4a0 H:|-(0)-[UIStackView:0x7f8dab569320]   (active, names: '|': XXXXX.MultipleBarGraphsView:0x7f8dab561190'What are you doing' )>",
    "<NSLayoutConstraint:0x61000028b3b0 UIStackView:0x7f8dab569320.trailing == XXXXX.MultipleBarGraphsView:0x7f8dab561190'Hur ofta svarar du r\U00e4tt?'.trailing   (active)>",
    "<NSLayoutConstraint:0x61000028b900 '_UITemporaryLayoutWidth' XXXXX.MultipleBarGraphsView:0x7f8dab561190'Hur ofta svarar du r\U00e4tt?'.width == 0   (active)>",
    "<NSLayoutConstraint:0x61000028af00 'UISV-canvas-connection' UIStackView:0x7f8dab569320.leading == UIView:0x7f8dab566620.leading   (active)>",
    "<NSLayoutConstraint:0x61000028bbd0 'UISV-canvas-connection' H:[UIView:0x7f8dab566620]-(0)-|   (active, names: '|':UIStackView:0x7f8dab569320 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x61000028b6d0 UIView:0x7f8dab565720.trailing == UIView:0x7f8dab566620.trailing - 30   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

答案 1 :(得分:4)

您可以使用

 $myData['count'] = $final_count;
 $myData['dates'] = $items
 echo json_encode($myData);

答案 2 :(得分:2)

当然,您可以使用所有收集数据的array_merge。

或者:

$data=[];
// get $final_count
$data['count'] = $final_count;
// ... do some more stuff
// load items from db
$data['dates'] = $items;

echo json_encode($data);
相关问题