Jump to content


This topic is now archived and is closed to further replies.


object oriented and array_walk_recursive

Recommended Posts


I want to walk an array and handle every element I encounter with a function.
There is a problem when I use [url=http://fr.php.net/array-walk-recursive][b]array-walk-recursive[/b]
[/url] and objects.

The code works fine like it is on the php manual site but doesn't when I use Objects.
Can someone help me and tell what I am doeing wrong.

I hope it isn't a problem when I post the code here. If it does, I apoligize.

the Error
Warning: array_walk_recursive() [function.array-walk-recursive]:
[!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]Unable to call test_print() - function does not exist in[!--colorc--][/span][!--/colorc--]
C:\Program Files\xampp\htdocs\www\arrayrunner.php on line 11 (and every line were rray-walk-recursive is called)

The code
$anObject = new ClassName();

[b]array_walk_recursive[/b]($anObject->getFruits(), 'test_print');
[!--coloro:#C0C0C0--][span style=\"color:#C0C0C0\"][!--/coloro--]//or[!--colorc--][/span][!--/colorc--]
[b]array_walk_recursive[/b]($anObject->getFruits(), '$anObject->test_print');
[!--coloro:#C0C0C0--][span style=\"color:#C0C0C0\"][!--/coloro--]//or...the same but in alternative way (see class)[!--colorc--][/span][!--/colorc--]

//Class description---------------------------------------------
class ClassName {
//Class variables
private $sweet = array();
private $fruits = array();

public function __construct(){
$this->sweet = array('a' => 'apple', 'b' => 'banana');
$this->fruits = array('sweet' => $sweet, 'sour' => 'lemon');

//public methods
public function [!--coloro:#3366FF--][span style=\"color:#3366FF\"][!--/coloro--][b]test_print[/b][!--colorc--][/span][!--/colorc--]($item, $key){
echo "$key holds $item\n<br>";

public function [b]arrWalker[/b]($arr){
[!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]/*I get errors that he doesn't know test_print*/[!--colorc--][/span][!--/colorc--]
array_walk_recursive($arr, '$this->test_print');
array_walk_recursive($arr, 'test_print');

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.