note 106866 added to function.array-map

From: Date: Wed, 14 Dec 2011 01:53:59 +0000
Subject: note 106866 added to function.array-map
Groups: php.notes 
Request: Send a blank email to php-notes+get-185072@lists.php.net to get a copy of this message
I needed to enlarge the use of array_map based on erik dot stetina at gmail dot com example, eg to add a suffix or a prefix string to multidimensional arrays with unknown structure. <?php class Tools { /** * Add recursively a suffix or a prefix string to all of the (assoc) array values * Created 13/12/2011 * @see http://www.php.net/manual/en/function.array-map.php#105942 */ static function array_mod_values_recurse($string, $position="suffix", array $array) { switch ($position) { case "suffix": $callback = create_function('$str', 'return $str. "'. $string . '";' ); break; case "prefix": $callback = create_function('$str','return "'.$string.'".$str;'); break; default: $callback = create_function('$str', 'return $str. "'. $string . '";' ); break; } foreach ($array as $key => $value) { if (is_array($value)){ $result[] = Tools::array_mod_values_recurse($string, $position, $value); } else { $result[] = array_map($callback, $array); } } return $result; } } ?> Usage example: <?php $txtbox = array( array("FR"=> "Prénom", "EN"=> "First Name", "IT"=> "Nome"), array("FR"=> "Nom", "EN"=> "Name", "IT"=> "Cognome")); $string = " : "; $position ="suffix"; $txtbox = Tools::array_mod_values_recurse($string, $position, $txtbox); // DEBUG echo '<br/>RESULT<pre>'; print_r($txtbox);echo '</pre>'; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 82.246.68.6 ---- Manual Page -- http://www.php.net/manual/en/function.array-map.php Edit -- https://master.php.net/note/edit/106866 Del: integrated -- https://master.php.net/note/delete/106866/integrated Del: useless -- https://master.php.net/note/delete/106866/useless Del: bad code -- https://master.php.net/note/delete/106866/bad+code Del: spam -- https://master.php.net/note/delete/106866/spam Del: non-english -- https://master.php.net/note/delete/106866/non-english Del: in docs -- https://master.php.net/note/delete/106866/in+docs Del: other reasons-- https://master.php.net/note/delete/106866 Reject -- https://master.php.net/note/reject/106866 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185072) next »