note 109374 deleted from function.array-walk by danbrown
| From: | danbrown@php.net | Date: | Mon, 16 Jul 2012 15:36:30 +0000 |
| Subject: | note 109374 deleted from function.array-walk by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189190@lists.php.net to get a copy of this message | ||
Note Submitter: mwwaygoo AT hotmail DOT com
----
further to paul at heliosville dot com and a correction of memandeemail at gmail dot com
Inside a class you can array walk a method, by supplying $this and the name of the method, as an
array for the second argument.
<?php
class test {
public function my_function(&$data) {
$data=1;
}
public function clean_data($data) {
array_walk($data, array($this, "my_function"));
return $data;
}
}
$a=new test();
$a->clean_data($my_array);
?>
NOTE: to reference the current method, you can use __FUNCTION__ instead of the literal name.
But avoid __METHOD__ as this will also include the name of the class (ie test::my_function), which
is not required for instantiated classes like this.
<?php
class test {
public function objectToArray(&$data)
{
if(is_object($data))
{
$data=get_object_vars($data);
}
if(is_array($data))
{
array_walk($data, array(&$this, __FUNCTION__));
}
}
}
$a=new test();
$a->objectToArray($my_object);
?>