search for in the  
<array_walk_recursivearray>
Last updated: Thu, 19 May 2005

array_walk

(PHP 3 >= 3.0.3, PHP 4, PHP 5)

array_walk --  Apply a user function to every member of an array

Description

bool array_walk ( array &array, callback funcname [, mixed userdata] )

Returns TRUE on success or FALSE on failure.

Applies the user-defined function funcname to each element of the array array. Typically, funcname takes on two parameters. The array parameter's value being the first, and the key/index second. If the optional userdata parameter is supplied, it will be passed as the third parameter to the callback funcname.

If function funcname requires more parameters than given to it, an error of level E_WARNING will be generated each time array_walk() calls funcname. These warnings may be suppressed by prepending the PHP error operator @ to the array_walk() call, or by using error_reporting().

Note: If funcname needs to be working with the actual values of the array, specify the first parameter of funcname as a reference. Then, any changes made to those elements will be made in the original array itself.

Note: Passing the key and userdata to funcname was added in 4.0.0

array_walk() is not affected by the internal array pointer of array. array_walk() will walk through the entire array regardless of pointer position. To reset the pointer, use reset(). In PHP 3, array_walk() resets the pointer.

Users may not change the array itself from the callback function. e.g. Add/delete elements, unset elements, etc. If the array that array_walk() is applied to is changed, the behavior of this function is undefined, and unpredictable.

Example 1. array_walk() example

<?php
$fruits
= array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");

function
test_alter(&$item1, $key, $prefix)
{
  
$item1 = "$prefix: $item1";
}

function
test_print($item2, $key)
{
   echo
"$key. $item2<br />\n";
}

echo
"Before ...:\n";
array_walk($fruits, 'test_print');

array_walk($fruits, 'test_alter', 'fruit');
echo
"... and after:\n";

array_walk($fruits, 'test_print');
?>

The above example will output:

Before ...:
d. lemon
a. orange
b. banana
c. apple
... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple

See also array_walk_recursive(), create_function(), list(), foreach, each(), call_user_func_array(), and array_map(), and information about the callback type.



User Contributed Notes
array_walk
Enlightened One
08-Apr-2005 01:17
Beware that "array ($this, method)" construct. If you're wanting to alter members of the "$this" object inside "method" you should construct the callback like this:

  $callback[] = &$this;
  $callback[] = method;
  array_walk ($input, $callback);

Creating your callback using the array() method as suggested by "appletalk" results in a copy of $this being passed to method, not the original object, therefor any changes made to the object by method will be lost when array_walk() returns. While you could construct the callback with "array(&$this, method)", I believe this relies on the deprecated runtime pass-by-reference mechanism which may be removed in future releases of PHP. Better to not create a dependence on that feature now than having to track it down and fix it in the future.
Hayley Watson
16-Jan-2005 08:27
As well as being able to pass the array the callback will be working on by reference, one can pass the optional userdata parameters by reference also:
<?php

function surprise($x,$key,$xs)
{
  
//$key is unused here.
  
$x.='!';
  
array_push($xs,$x);
}

$array1 = array('this','that','the other');
$array2 = array();

array_walk($array1,'surprise',&$array2);

print_r($array1);
print_r($array2);
?>
Of course, that precise example would be better handled by array_map, but the principle is there.
appletalk at gmail dot com
23-Dec-2004 08:26
To use array_walk with a class simply do:
array_walk($input, array($this, method) );
memandeemail at gmail dot com
11-Nov-2004 07:24
If you are using array_walk on a class, dont will work

so ... try this on your own class:

class your_own_class {
   /**
   * @return void
   * @param array $input
   * @param string $funcname
   * @desc A little workaround, do the same thing.
   */
   function array_walk($input, $funcname) {
       foreach ($input as $key => $value) $this->$funcname($value, $key);
   }
}
05-Nov-2004 01:22
If array_walk_recursive() is not present and you want to apply htmlentities() on each array element you can use this:

function array_htmlentities(&$elem)
{
  if (!is_array($elem))
  {
   $elem=htmlentities($elem);
  }
  else
  {
   foreach ($elem as $key=>$value)
     $elem[$key]=array_htmlentities($value);
  }
  return $elem;
} // array_htmlentities()

If you want to output an array with print_r() and you have html in it this function is very helpful.
lgaga dot dont dot spam at muszaki dot info
16-Oct-2004 09:31
Behaviour like array_walk_recursive() can be achieved in php <=5 by a callback function to array_walk() similar to this:

function walkcallback(&$val,$key) {
   if (is_array($val)) array_walk($val,'walkcallback',$new);
   else {
       // do what you want with $val and $key recursively
   }
}
bisqwit at iki dot fi
03-Sep-2004 06:54
It's worth nothing that array_walk can not be used to change keys in the array.
The function may be defined as (&$value, $key) but not (&$value, &$key).
Even though PHP does not complain/warn, it does not modify the key.
paul at heliosville dot com
03-Sep-2004 02:13
one rather important note that was lost in the Great PHP Doc Note Purge of '04 is that you can call methods using array_walk(). Let's assume that we have a class named 'Search', in which there is a method called 'convertKeywords'. Here's how you would call that convertKeywords method from inside the class:

   array_walk($keywords, array($this, 'convertKeywords'));

Notice that, instead of giving a string as the second argument, you give an array with two items: the variable that holds the class (in this case, $this), and the method to call. Here's what it would look like if you were to call convertKeywords from an already-instantiated class:

   $search = new Search;
   array_walk($keywords, array($search, 'convertKeywords'));
Eierkoek
03-Sep-2004 08:46
normaly the $_GET array will add slashes to the array values. To remove all slashes in this array, i created the folowing code

  set_magic_quotes_runtime (0);
  function StripAllSlashes (&$ArrayGET, $Value)
  {
   if (is_array ($ArrayGET)) array_walk ($ArrayGET, "StripAllSlashes");
   else $ArrayGET = stripslashes ($ArrayGET);
  }
  if (isset ($_GET) && get_magic_quotes_gpc ()) array_walk ($_GET, "StripAllSlashes");

I hope this code was usefull,

Eierkoek

<array_walk_recursivearray>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT