user3498116
user3498116

Reputation: 31

How to remove single Quotes from parsing PHP Array into JSON?

Thats my result when i say echo json_encode($array):

[{"name":"test"}]

I'm searching for something like replace. have php a method for replacing strings in arrays? The result should be this:

[{name:test}]

Upvotes: 1

Views: 3420

Answers (2)

Sanjay Kumar N S
Sanjay Kumar N S

Reputation: 4739

You can do something like this:

<?php
$arr1 = array();
$i = 1;
function replace($key)
{
  echo str_replace('"', '', $key)."<br />\n";
}


 $fruits = array("d" => '"lemon', "a" => '"orange', "b" => "banana", "c" => "apple");
 array_walk($fruits, 'replace');
?>

Upvotes: 1

Kita
Kita

Reputation: 2634

PHP have str_replace() function.

$result = str_replace('"', '', json_encode($array));

Btw, according to JSON RFC, [{"name":"test"}] is the correct format. If you try to parse [{name:test}] in JavaScript, JS will emit "test is not defined" error.

Upvotes: 0

Related Questions