gieterke Posted March 27, 2006 Share Posted March 27, 2006 HelloI 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-----------<?php $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--] [b]$anObject->arrWalker[/b]($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--][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'); //or array_walk_recursive($arr, 'test_print'); }}?> Link to comment https://forums.phpfreaks.com/topic/5912-object-oriented-and-array_walk_recursive/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.