note 92815 deleted from function.str-replace by danbrown

From: Date: Mon, 10 Aug 2009 20:15:58 +0000
Subject: note 92815 deleted from function.str-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-159288@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- The function recursive_array_replace proposed by moz667 at gmail dot com isn't very efficient. If the value of an array is itself an array, the function is_array will be run on it twice. moreover, the else condition of the first if statement can only be true if on the original call to recursive_array_replace $data isn't an array; every other time it is run it must evaluate to true. Instead, I propose the function: function recursive_array_replace($find, $replace, &$data) { foreach($data as $key=>$value) { if(is_array($value)) { $data[$key] = rar2($find, $replace, $value); } else { $data[$key] = str_replace($find, $replace, $value); } } } If $data isn't actually an array then there will be a PHP warning on the foreach loop, but that's beside the point. In my tests on arrays of size ~6000 with multiple subarrays, this runs about 10x faster than the original recursive_array_replace.

« previous php.notes (#159288) next »