SwiftDeveloper
SwiftDeveloper

Reputation: 7370

Php json_encode Array output \ error

Hello i have php mysql pdo array with output json encode and gives me \\\ chars in output and i want to delete them.

My php code

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

And gives output

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

I want remove \ chars.

Thanks

Upvotes: 4

Views: 495

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

Change your code to include one forms. You are mixing JavaScript and PHP. So, do this:

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

Upvotes: 3

Related Questions