4

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

5
  • That's actually a valid JavaScript string, because, ... Wait what? Commented Feb 26, 2016 at 10:57
  • @PraveenKumar i will use output for swift app and dont take values gives error when have \ Commented Feb 26, 2016 at 10:58
  • You cannot JSON Encode an array of JSONs... LoL. Commented Feb 26, 2016 at 10:58
  • @PraveenKumar normally json output valid but have \ chars and gives on swift Commented Feb 26, 2016 at 10:59
  • 1
    You either cobble JSON together by hand or you use json_encode to do that for you; doing both results in garbage. Commented Feb 26, 2016 at 11:08

1 Answer 1

3

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']
);
Sign up to request clarification or add additional context in comments.

2 Comments

@SwiftDeveloper Enjoy! :)
You may remove the $private_list = array(); stupid declaration! LoL.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.