Page 1 of 1

array_walk with Class Level Function

Posted: Thu Jun 14, 2007 3:43 am
by TryAgain2
From inside a class, can array_walk() take a class level function as an argument? I know it can call a function outside of the class, but that seems untidy.

Code: Select all

class SomeClass
{

	private function sortIntoLists($value, $key)
	{
		echo  "Key $key Value $value <br />";
	}

	private function processText()
	{
		// Break the text into a word array with the index as their positions
		$this->mArrayAllWords = str_word_count($this->mTextForAnalysis, 2);

		// Walk through the array and sort the words into the appropriate lists		
		array_walk($this->mArrayAllWords,  "sortIntoLists"); // **** Function does not exist! ****
                  }
}
I also tried "\$this->sortIntoLists" and "$this->sortIntoLists" with the same error message.

Posted: Thu Jun 14, 2007 5:05 am
by volka

Code: Select all

array_walk($this->mArrayAllWords,  array($this, 'sortIntoLists') );

Posted: Thu Jun 14, 2007 5:57 am
by TryAgain2
Thanks Volka