note 116228 added to function.array-replace-recursive

From: Date: Tue, 25 Nov 2014 20:20:50 +0000
Subject: note 116228 added to function.array-replace-recursive
Groups: php.notes 
Request: Send a blank email to php-notes+get-201020@lists.php.net to get a copy of this message
If you came here looking for a function to recursively find and replace (scalar) values in an array (also recurses through objects): <?php define('RECURSIVE_REPLACE_MARKER', 'recursive_replace_r47yr74yr7623t74r3645236rtvghdcbnsgxbt67g5e21873891'); /** * Recursively replaces scalar values in objects and arrays. The passed array or object is altered in place. * @param mixed $data An object or array. * @param mixed $find The scalar value to find. * @param mixed $replace The value to replace with (need not be a scalar). */ function recursive_replace(&$data, $find, $replace){ if(is_array($data)) { if (!isset($data[RECURSIVE_REPLACE_MARKER])) { $data[RECURSIVE_REPLACE_MARKER] = TRUE; foreach($data as $key=>$val) { if(is_array($data[$key]) || is_object($data[$key])) { recursive_replace($data[$key], $find, $replace); } else{ if($data[$key] === $find) { $data[$key] = $replace; } } } unset($data[RECURSIVE_REPLACE_MARKER]); } } elseif (is_object($data)) { if (!isset($data->RECURSIVE_REPLACE_MARKER)) { $data->RECURSIVE_REPLACE_MARKER = TRUE; foreach($data as $key=>$val) { if(is_array($data->$key) || is_object($data->$key)){ recursive_replace($data->$key, $find, $replace); }else{ if($data->$key === $find) { $data->$key = $replace; } } } unset($data->RECURSIVE_REPLACE_MARKER); } } } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 220.233.123.171 ---- Manual Page -- http://php.net/manual/en/function.array-replace-recursive.php Edit -- https://master.php.net/note/edit/116228 Del: integrated -- https://master.php.net/note/delete/116228/integrated Del: useless -- https://master.php.net/note/delete/116228/useless Del: bad code -- https://master.php.net/note/delete/116228/bad+code Del: spam -- https://master.php.net/note/delete/116228/spam Del: non-english -- https://master.php.net/note/delete/116228/non-english Del: in docs -- https://master.php.net/note/delete/116228/in+docs Del: other reasons-- https://master.php.net/note/delete/116228 Reject -- https://master.php.net/note/reject/116228 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#201020) next »