note 81989 deleted from function.array-walk by danbrown
| From: | danbrown@php.net | Date: | Thu, 28 Oct 2010 16:18:44 +0000 |
| Subject: | note 81989 deleted from function.array-walk by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173563@lists.php.net to get a copy of this message | ||
Note Submitter: stroybomic
----
hioctiane at hioctiane dot de and ibolmo
<?php
class walker
{
public function walk(&$str_or_arr, $method, $string_or_object = NULL)
{
if ( is_array($string_or_object) )
{
$this->an_array(&$str_or_arr, $method, $string_or_object);
}
elseif ( is_string($string_or_object) )
{
$this->a_string(&$str_or_arr, $method, $string_or_object);
}
else
{
return false;
}
}
private function an_array(&$array, $method, $object = NULL)
{
//when argument object is given
if ( isset($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;
}
}
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;
}
private function a_string(&$string, $funcname, $userdata = NULL)
{
for($i = 0; $i < strlen($string); $i++)
{
# NOTE: PHP's dereference sucks, we have to do this.
$hack = $string{$i};
call_user_func($funcname, &$hack, $i, $userdata);
$string{$i} = $hack;
}
}
}
?>