note 92815 added to function.str-replace

From: Date: Mon, 10 Aug 2009 20:08:05 +0000
Subject: note 92815 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-159286@lists.php.net to get a copy of this message
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. ---- Server IP: 64.71.164.2 Probable Submitter: 70.129.225.173 ---- Manual Page -- http://www.php.net/manual/en/function.str-replace.php Edit -- https://master.php.net/note/edit/92815 Del: integrated -- https://master.php.net/note/delete/92815/integrated Del: useless -- https://master.php.net/note/delete/92815/useless Del: bad code -- https://master.php.net/note/delete/92815/bad+code Del: spam -- https://master.php.net/note/delete/92815/spam Del: non-english -- https://master.php.net/note/delete/92815/non-english Del: in docs -- https://master.php.net/note/delete/92815/in+docs Del: other reasons-- https://master.php.net/note/delete/92815 Reject -- https://master.php.net/note/reject/92815 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#159286) next »