2016-02-26 82 views
4

你好,我有php輸出json編碼的pdo數組,並給出了\\\字符輸出,我想delete他們。Php json_encode數組輸出錯誤

我的PHP代碼

$stmt2 = $this->conn->prepare("SELECT ID,clientName FROM Clients WHERE userID='$userID' OR mainAccountID='$mainAccountID' ORDER BY ID DESC"); 
$stmt2->execute(); 
$result = $stmt2 -> fetchAll(); 

foreach($result as $userRow2) { 
    $private_list[] = '{"name":"'.$userRow2['clientName'].'","ID":"'.$userRow2['ID'].'"}'; 
} 

echo json_encode($private_list); 

並給出了輸出

["{\"name\":\"zz\",\"ID\":\"312\"}","{\"name\":\"jv\",\"ID\":\"311\"}","{\"name\":\"fff2222\",\"ID\":\"309\"}","{\"name\":\"ffff\",\"ID\":\"308\"}","{\"name\":\"v\",\"ID\":\"288\"}","{\"name\":\"t\",\"ID\":\"286\"}","{\"name\":\"s\",\"ID\":\"285\"}","{\"name\":\"r\",\"ID\":\"284\"}","{\"name\":\"p\",\"ID\":\"283\"}","{\"name\":\"o\",\"ID\":\"282\"}","{\"name\":\"n\",\"ID\":\"281\"}","{\"name\":\"m\",\"ID\":\"280\"}","{\"name\":\"l\",\"ID\":\"279\"}","{\"name\":\"k\",\"ID\":\"278\"}","{\"name\":\"j\",\"ID\":\"277\"}","{\"name\":\"i\",\"ID\":\"276\"}","{\"name\":\"h\",\"ID\":\"275\"}","{\"name\":\"g\",\"ID\":\"274\"}","{\"name\":\"f\",\"ID\":\"273\"}","{\"name\":\"e\",\"ID\":\"272\"}","{\"name\":\"d\",\"ID\":\"271\"}","{\"name\":\"c\",\"ID\":\"270\"}","{\"name\":\"b\",\"ID\":\"269\"}","{\"name\":\"a\",\"ID\":\"268\"}"] 

我想刪除\字符。

謝謝

+0

這實際上是一個有效的JavaScript字符串,因爲......等等什麼? –

+0

@PraveenKumar我將使用輸出爲快速的應用程序,並不採取值給出錯誤時有\ – SwiftDeveloper

+0

你不能JSON編碼的JSON數組... ... LoL。 –

回答

3

更改您的代碼以包含一種形式。你在混合JavaScript和PHP。所以,這樣做:

$private_list = array(); 
$private_list[] = array(
    "name" => $userRow2['clientName'], 
    "ID" => $userRow2['ID'] 
); 
+0

謝謝老兄工作!我會接受這個答案! – SwiftDeveloper

+0

@Swift開發者享受! ':'' –

+0

你可以刪除'$ private_list = array();'stupid聲明!大聲笑。 –