Jump to content


Photo

object oriented and array_walk_recursive


  • Please log in to reply
No replies to this topic

#1 gieterke

gieterke
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 27 March 2006 - 08:05 AM

Hello

I want to walk an array and handle every element I encounter with a function.
There is a problem when I use array-walk-recursive
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
-----------
<?php
$anObject = new ClassName();

array_walk_recursive($anObject->getFruits(), 'test_print');
[!--coloro:#C0C0C0--][span style=\"color:#C0C0C0\"][!--/coloro--]//or[!--colorc--][/span][!--/colorc--]
array_walk_recursive($anObject->getFruits(), '$anObject->test_print');
[!--coloro:#C0C0C0--][span style=\"color:#C0C0C0\"][!--/coloro--]//or...the same but in alternative way (see class)[!--colorc--][/span][!--/colorc--]
$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' => $sweet, 'sour' => 'lemon');
}

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

public function arrWalker($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');
//or
array_walk_recursive($arr, 'test_print');
}
}
?>




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users