Voting

: seven minus six?
(Example: nine)

The Note You're Voting On

nikolaz dot tang at hotmail dot com
14 years ago
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:

<?php
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:

<?php
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);
}
}
?>

<< Back to user notes page

To Top