如何将元素添加到php数组而不显示为新数组

时间:2016-12-25 20:15:09

标签: php mysql arrays

以下代码的意图是从mysql表中提取消息,并将每个消息放在一个数组中,每个输出周围都有{}。如您所见,每个输出都包含各种参数,并且本身就是一个数组。 代码所做的是每次处理循环时,在稍后转换为的JSON数组中,它将输出包装在[]中,因此它现在是一个新的数组。 我得到的是:

[{"sender":"ll","message":"blah","timestamp":"2016-12-21 14:43:04","username":"","msgtype":"","threadid":"32629016712222016034323"},{"sender":"kk","message":"blahblah","timestamp":"2016-12-21 14:43:23","username":"","msgtype":"","threadid":"32629016712222016034323"},{"sender":"ll","message":"blahblahblah","timestamp":"2016-12-21 14:43:47","username":"","msgtype":"","threadid":"32629016712222016034323"}],[{"sender":"ll","message":"blahblahblahblah","timestamp":"2016-12-21 14:43:04","username":"","msgtype":"","threadid":"92337321312222016034304"},{"sender":"kk","message":"blahblahblahblahblah","timestamp":"2016-12-21 14:44:05","username":"","msgtype":"","threadid":"92337321312222016034304"}]]

我想要的是:

[{"sender":"ll","message":"blah","timestamp":"2016-12-21 14:43:04","username":"","msgtype":"","threadid":"32629016712222016034323"},{"sender":"kk","message":"blahblah","timestamp":"2016-12-21 14:43:23","username":"","msgtype":"","threadid":"32629016712222016034323"},{"sender":"ll","message":"blahblahblah","timestamp":"2016-12-21 14:43:47","username":"","msgtype":"","threadid":"32629016712222016034323"}],{"sender":"ll","message":"blahblahblahblah","timestamp":"2016-12-21 14:43:04","username":"","msgtype":"","threadid":"92337321312222016034304"},{"sender":"kk","message":"blahblahblahblahblah","timestamp":"2016-12-21 14:44:05","username":"","msgtype":"","threadid":"92337321312222016034304"}]

如何在此处获得正确的结果?

$data = array ();
foreach($threads as $threadid){
$sql = ("SELECT sender,message,timestamp,username,msgtype,threadid FROM    Messages WHERE  threadid = '$threadid' AND subject = '' AND timestamp >    '$newtimestamp' ORDER BY timestamp");
$arrayOfObjects = $conn->query($sql)->fetchAll(PDO::FETCH_OBJ);
$data[] = $$arrayOfObjects;
}

而且,FYI,$ threadid是另一个包含... threadids的数组,并且循环正确地逐个取出这些,这不是问题所在。

提前致谢!!

3 个答案:

答案 0 :(得分:1)

您正在进行O(N)数据库查询,请考虑在where子句中使用O(1)表达式执行IN。无需foreach循环,您将获得所有数据。

SELECT ... FROM Messages WHERE threadid IN (1, 2, 3, ...) AND ...

您可能必须使用准备好的声明。

答案 1 :(得分:0)

我认为您正在搜索PDO::FETCH_OBJ。 你有FETCH_ASSOC,它将返回一组关联数组 FETCH_OBJ将返回一个数组ob stdObjects。

此外,您在执行$array时将$array[] = $array;重新分配给自己。

$data = array();
foreach($threads as $threadid){
    $sql = ("SELECT sender,message,timestamp,username,msgtype,threadid FROM    Messages WHERE  threadid = '$threadid' AND subject = '' AND timestamp >    '$newtimestamp' ORDER BY timestamp");
    //                                             here it is:
    $arrayOfObjects = $conn->query($sql)->fetchAll(PDO::FETCH_OBJ);
    $data[] = $arrayOfObjects;
}

// now you can encode that as json and show it:
echo json_encode($data);

答案 2 :(得分:0)

@akuhn

好吧,我决定再试一次你的建议,并设法以一种毫无准备的方式做到这一点。我知道这应该是有风险的,但到目前为止这个项目只需要工作,然后将PHP代码更新为更安全的版本,然后上线。它有效,所以非常感谢!

$sql = ("SELECT sender,message,timestamp,username,msgtype,threadid FROM Messages WHERE threadid IN ('" . implode("','",$threadid) . "') AND subject = '' AND timestamp > '$newtimestamp' ORDER BY timestamp");
$data = $conn->query($sql)->fetchAll(PDO::FETCH_OBJ);