2016-12-25 69 views
1

下面代碼的用意是從mysql表中提取消息,並將每個消息放在每個輸出周圍帶有{}的一個數組中。如您所見,每個輸出都由各種參數組成,並且本身就是一個數組。 代碼的作用是每次處理循環時,在後面的JSON數組中將其轉換爲,它將輸出包裝在[]中,因此它現在是一個新創建的數組。 我得到的是:如何將元素添加到php數組而不顯示爲新數組

[{"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; 
} 

,僅供參考,$線程ID是一個包含另一個數組...線程ID,並且循環正確獲取這些一個接一個,that's問題並不在那裏。

在此先感謝!

+0

* ...並將它們中的每一個放在每個輸出周圍的一個數組中{},您是否試圖獲取json字符串? –

+0

你能指定你的意思嗎?「在每個輸出周圍用{}排列一個陣列」_?你在哪裏/如何看待這些括號?作爲Json也許?由於{}表示一個對象,而不是一個數組,所以你想要的東西有點混亂。 – Jeff

+0

您需要在foreach之外定義空數組var,並將數組內容推送到foreach中。 – Eugen

回答

1

你正在做O(N)數據庫查詢再分配$array本身,考慮使用IN表達這樣做只是O(1)您的where子句。不需要foreach循環,你將得到一個數組中的所有數據。

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

您可能必須爲此使用預準備語句。

+0

所以「WHERE threadid IN('$ threadid')」?然後我只是得到[]作爲輸出。 –

+0

'IN'需要逗號分隔的ALL ID列表。 – akuhn

+0

因此,這種方法不能工作,因爲threadids需要從mysql數據庫中提取。或者有沒有辦法在PHP內而不是數組中生成這樣一個逗號分隔的列表,並按照我所做的方式使用它? –

0

我認爲您正在尋找PDO::FETCH_OBJ。 你有FETCH_ASSOC,它將返回一個關聯數組數組。
FETCH_OBJ將返回一個數組obstdObjects。

你也做$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); 
+0

這確實是一個JSON字符串,我打算在這裏對不起,不太清楚。 –

+0

看到我的編輯。那是你想要的嗎? – Jeff

+0

我嘗試了所有的修改,但我得到了相同的結果。我也更新了最初的問題,所以你可以看到我說的是什麼。 –

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