note 63731 added to function.array-walk-recursive
| From: | gieterkeatgmaildotcom at osu1 dot php dot net | Date: | Wed, 29 Mar 2006 12:59:59 +0000 |
| Subject: | note 63731 added to function.array-walk-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-106942@lists.php.net to get a copy of this message | ||
I think there are a lot of people who want/have to work object oriented.
this is an Object oriented version
the code is written in PHP5
<?php
//auto overload classes, so you don't have to put in all the includes
function __autoload($class_name) {
require_once $class_name . '.php';
}
echo "TESTING: arraywalker<br>";
$anObject = new ClassName();
//array_walk_recursive($anObject->getFruits(), 'test_print'); => doesn't work
array_walk_recursive($anObject->getFruits(), array(&$anObject, 'test_print'));
echo"<br><br>";
//if you just want to give the array and not hte function name that handles the array
$anObject->arrWalker($anObject->getFruits());
?>
<?php
//Class description---------------------------------------------
class ClassName {
//Class variables
private $sweet = array();
private $fruits = array();
//Constructor
public function __construct(){
$this->sweet = array('a' => 'apple', 'b' =>
'banana');
$this->fruits = array('sweet' => $this->sweet, 'sour' =>
'lemon');
}
//public methods
public function getFruits(){
return $this->fruits;
}
public function test_print($item, $key){
echo "$key holds $item\n<br>";
}
public function arrWalker($arr){
array_walk_recursive($arr, array(&$this, 'test_print'));
}
}
?>
the code above gives following input:
TESTING: arraywalker
a holds apple
b holds banana
sour holds lemon
a holds apple
b holds banana
sour holds lemon
----
Server IP: 194.246.101.61
Probable Submitter: 213.56.43.81
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.array-walk-recursive.php
Edit -- http://master.php.net/note/edit/63731
Del: integrated -- http://master.php.net/note/delete/63731/integrated
Del: useless -- http://master.php.net/note/delete/63731/useless
Del: bad code -- http://master.php.net/note/delete/63731/bad+code
Del: spam -- http://master.php.net/note/delete/63731/spam
Del: non-english -- http://master.php.net/note/delete/63731/non-english
Del: in docs -- http://master.php.net/note/delete/63731/in+docs
Del: other reasons-- http://master.php.net/note/delete/63731
Reject -- http://master.php.net/note/reject/63731
Search -- http://master.php.net/manage/user-notes.php