note 71915 added to function.array-walk
| From: | hioctiane at hioctiane dot de | Date: | Fri, 22 Dec 2006 10:00:28 +0000 |
| Subject: | note 71915 added to function.array-walk | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120846@lists.php.net to get a copy of this message | ||
<?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
)
----
Server IP: 217.13.201.10
Probable Submitter: 212.80.233.188 (proxied: unknown)
----
Manual Page -- http://www.php.net/manual/en/function.array-walk.php
Edit -- https://master.php.net/note/edit/71915
Del: integrated -- https://master.php.net/note/delete/71915/integrated
Del: useless -- https://master.php.net/note/delete/71915/useless
Del: bad code -- https://master.php.net/note/delete/71915/bad+code
Del: spam -- https://master.php.net/note/delete/71915/spam
Del: non-english -- https://master.php.net/note/delete/71915/non-english
Del: in docs -- https://master.php.net/note/delete/71915/in+docs
Del: other reasons-- https://master.php.net/note/delete/71915
Reject -- https://master.php.net/note/reject/71915
Search -- https://master.php.net/manage/user-notes.php