note 62172 added to function.array-walk-recursive
| From: | gabrielu at hotmail dot com | Date: | Tue, 21 Feb 2006 21:13:18 +0000 |
| Subject: | note 62172 added to function.array-walk-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-104162@lists.php.net to get a copy of this message | ||
I decided to add to the previous PHP 4 compatible version of array_walk_recursive() so that it would
work within a class and as a standalone function. Both instances are handled by the following
function which I modified from omega13a at sbcglobal dot net.
The following example is for usage within a class. To use as a standalone function take it out of
the class and rename it. (Example: array_walk_recursive_2)
<?php
class A_Class {
function array_walk_recursive(&$input, $funcname, $userdata = '') {
if(!function_exists('array_walk_recursive')) {
if(!is_callable($funcname))
return false;
if(!is_array($input))
return false;
foreach($input as $key=>$value) {
if(is_array($input[$key])) {
if(isset($this)) {
eval('$this->' . __FUNCTION__ . '($input[$key], $funcname,
$userdata);');
} else {
if(@get_class($this))
eval(get_class() . '::' . __FUNCTION__ . '($input[$key], $funcname,
$userdata);');
else
eval(__FUNCTION__ . '($input[$key], $funcname, $userdata);');
}
} else {
$saved_value = $value;
if(is_array($funcname)) {
$f = '';
for($a=0; $a<count($funcname); $a++)
if(is_object($funcname[$a])) {
$f .= get_class($funcname[$a]);
} else {
if($a > 0)
$f .= '::';
$f .= $funcname[$a];
}
$f .= '($value, $key' . (!empty($userdata) ? ', $userdata' :
'') . ');';
eval($f);
} else {
if(!empty($userdata))
$funcname($value, $key, $userdata);
else
$funcname($value, $key);
}
if($value != $saved_value)
$input[$key] = $value;
}
}
return true;
} else {
array_walk_recursive($input, $funcname, $userdata);
}
}
function kv_addslashes(&$v, $k) {
$v = addslashes($v);
}
}
?>
Usage:
<?php
$arr = array(
'a' => '"Hello World"',
'b' => "'Hello World'",
'c' => "Hello 'Worl\"d",
'd' => array(
'A' => 'H"e"l"l"o"
"W"o"r"l"d'
)
);
$class = new A_Class();
$class->array_walk_recursive($arr, array(&$class, 'kv_addslashes'));
print_r($arr);
?>
----
Server IP: 216.194.113.175
Probable Submitter: 68.194.110.148
----
Manual Page -- http://www.php.net/manual/en/function.array-walk-recursive.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+62172
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62172&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62172&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62172&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62172&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62172&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62172&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62172&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+62172&report=yes
Search -- http://master.php.net/manage/user-notes.php