note 100871 added to function.str-replace
| From: | nikolaz dot tang at hotmail dot com | Date: | Fri, 12 Nov 2010 04:57:04 +0000 |
| Subject: | note 100871 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173963@lists.php.net to get a copy of this message | ||
A faster way to replace the strings in multidimensional array is to json_encode() it, do the
str_replace() and then json_decode() it, like this:
function str_replace_json($search, $replace, $subject){
return json_decode(str_replace($search, $replace, json_encode($subject)));
}
This method is almost 3x faster (in 10000 runs.) than using recursive calling and looping method,
and 10x simpler in coding.
Compared to:
function str_replace_deep($search, $replace, $subject)
{
if (is_array($subject))
{
foreach($subject as &$oneSubject)
$oneSubject = str_replace_deep($search, $replace, $oneSubject);
unset($oneSubject);
return $subject;
} else {
return str_replace($search, $replace, $subject);
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 222.166.181.122
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/100871
Del: integrated -- https://master.php.net/note/delete/100871/integrated
Del: useless -- https://master.php.net/note/delete/100871/useless
Del: bad code -- https://master.php.net/note/delete/100871/bad+code
Del: spam -- https://master.php.net/note/delete/100871/spam
Del: non-english -- https://master.php.net/note/delete/100871/non-english
Del: in docs -- https://master.php.net/note/delete/100871/in+docs
Del: other reasons-- https://master.php.net/note/delete/100871
Reject -- https://master.php.net/note/reject/100871
Search -- https://master.php.net/manage/user-notes.php