note 94053 modified in function.array-map by danbrown

From: Date: Wed, 14 Oct 2009 04:10:07 +0000
Subject: note 94053 modified in function.array-map by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-161715@lists.php.net to get a copy of this message
Fixed a bug with array recursion. <?php /** * arrayMap function. Customized array_map function which preserves keys/associate array indexes. Note that this costs a descent amount more memory (eg. 1.5k per call) * * @access public * @param callback $callback Callback function to run for each element in each array. * @param mixed $arr1 An array to run through the callback function. * @param array $array Variable list of array arugments to run through the callback function. * @return array Array containing all the elements of $arr1 after applying the callback function to each one, recursively, maintain keys. */ function arrayMap($callback,$arr1) { $results = array(); $args = array(); if(func_num_args()>2) $args = (array) array_shift(array_slice(func_get_args(),2)); foreach($arr1 as $key=>$value) { $temp = $args; array_unshift($temp,$value); if(is_array($value)) { array_unshift($temp,$callback); $results[$key] = call_user_func_array(array('self','arrayMap'),$temp); } else { $results[$key] = call_user_func_array($callback,$temp); } } return $results; } ?> --was-- Fixed a bug with array recursion. /** * arrayMap function. Customized array_map function which preserves keys/associate array indexes. Note that this costs a descent amount more memory (eg. 1.5k per call) * * @access public * @param callback $callback Callback function to run for each element in each array. * @param mixed $arr1 An array to run through the callback function. * @param array $array Variable list of array arugments to run through the callback function. * @return array Array containing all the elements of $arr1 after applying the callback function to each one, recursively, maintain keys. */ function arrayMap($callback,$arr1) { $results = array(); $args = array(); if(func_num_args()>2) $args = (array) array_shift(array_slice(func_get_args(),2)); foreach($arr1 as $key=>$value) { $temp = $args; array_unshift($temp,$value); if(is_array($value)) { array_unshift($temp,$callback); $results[$key] = call_user_func_array(array('self','arrayMap'),$temp); } else { $results[$key] = call_user_func_array($callback,$temp); } } return $results; } http://php.net/manual/en/function.array-map.php

« previous php.notes (#161715) next »