note 71915 deleted from function.array-walk by danbrown

From: Date: Thu, 28 Oct 2010 16:19:04 +0000
Subject: note 71915 deleted from function.array-walk by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-173565@lists.php.net to get a copy of this message
Note Submitter: hioctiane at hioctiane dot de ---- <?php /*Thanks to Jerk, thats what I need all the time. I have a little Upgrade to your code. Now the values of your $array will be handled by a consign method from a consign object(optional, otherwise taking this operator).*/ class user_function { function user_function() { } /** * * handle all values from multidimensional array by $function from $object * */ function walk_multi_array(&$array, $method, $object=NULL) { //when argument object is given if (is_object($object)) { //check if it is object if (!is_object($object)) return false; //check if object has method if (!array_search(strtolower($method), get_class_methods(get_class($object)))) return false; } //no argument object is given else { //check if this class has method if (!array_search(strtolower($method), get_class_methods(get_class($this)))) return false; } foreach ($array as $key => $value) { //$value is array if(is_array($value)) //recursiv call of walk_multi_array $result[$key] = $this->walk_multi_array(&$value, $method, $object); //$value is value else { if (is_object($object)) $result[$key] = $object->$method($value); else $result[$key] = $this->$method($value); } } return $result; } } class test_class { function test_class() { } function strtoup($value) { return strtoupper($value); } } $test_class = new test_class; $user_function = new user_function; $array = array ( 1 => "testFall1", 2 => array ( 21 => "tesTfaLL21", 22 => array ( 221 => "TESTFALL221", 222 => "testfall222" ) ), 3 => "testfall3" ); echo "<pre>"; print_r($user_function->walk_multi_array($array, 'strtoup', $test_class)); echo "</pre>"; ?> Output: Array ( [1] => TESTFALL1 [2] => Array ( [21] => TESTFALL21 [22] => Array ( [221] => TESTFALL221 [222] => TESTFALL222 ) ) [3] => TESTFALL3 )

« previous php.notes (#173565) next »