user3432152
user3432152

Reputation: 47

Php double quotes in array

I want to use a few consecutive string replace functions.

Here is what I use:

$data_string = json_encode($data);
$newData = str_replace('\\', '', $data_string);
$NnewData = str_replace('"[', '[', $newData);
$FinalData = str_replace(']"', ']', $NnewData);

I'm looking for an alternative solution.

Upvotes: 1

Views: 1492

Answers (2)

John Conde
John Conde

Reputation: 219804

I don't know why you're doing what you're doing but you can make that a one-liner:

$final = str_replace(array('\\','"[', ']"'), array('', '[',']'), $data_string);

Basically str_replace() can take arrays for the search and replace parameters.

From the manual:

If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then this replacement string is used for every value of search.

Upvotes: 2

Jscti
Jscti

Reputation: 14440

Maybe you should take a look at the PHP doc ?

str_replace takes array in parameter ...

http://www.php.net/manual/en/function.str-replace.php

Upvotes: 2

Related Questions