★ wanayoo — archive 1999 http://www.php.net/manual/de/function.array-walk.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Arrays
Quick Reference
English version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Arrays
* array
* array_count_values
* array_flip
* array_keys
* array_merge
* array_merge_recursive
* array_multisort
* array_pad
* array_pop
* array_push
* array_rand
* array_reverse
* array_shift
* array_slice
* array_splice
* array_unshift
* array_values
* array_walk
* arsort
* asort
* compact
* count
* current
* each
* end
* extract
* in_array
* key
* krsort
* ksort
* list
* next
* pos
* prev
* range
* reset
* rsort
* shuffle
* sizeof
* sort
* uasort
* uksort
* usort
Manual: array_walk
View the source code for this pageSearch the site



Previous page
 array_values
 Updated
Sat, 12 Aug 2000
arsort 
Next page


array_walk

(PHP3 >= 3.0.3, PHP4 )

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

Description

int array_walk (array arr, string func, mixed userdata)

Applies the function named by func to each element of arr. func will be passed array value as the first parameter and array key as the second parameter. If userdata is supplied, it will be passed as the third parameter to the user function. If func requires more than two or three arguments, depending on userdata, a warning will be generated each time array_walk() calls func. These warnings may be suppressed by prepending the '@' sign to the array_walk() call, or by using error_reporting().

Anmerkung: If func needs to be working with the actual values of the array, specify that the first parameter of func should be passed by reference. Then any changes made to those elements will be made in the array itself.

Anmerkung: Passing the key and userdata to func was added in 4.0.

Beispiel 1. array_walk() example


$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";
}

array_walk( $fruits, 'test_print' );
array_walk( $fruits, 'test_alter', 'fruit' );
array_walk( $fruits, 'test_print' );
      

See also each() and list().


User Contributed Notes: array_walk


tone@resounding.com
07-Jun-1999 08:56
BIG OMISSION HERE!

It would be *extremely* valuable to be able to hand in another argument (or any number) to the function processing the elements of the array.

e.g.:

<pre>
function bug_smasher($element, $squish_me) {
if ($element == $sqish_me)
$element = "splat";
}

$ar = array("ant", "gerbil", "giraffe");
array_walk($ar, "bug_remover", "ant");
</pre>



cataldoj@ccs.neu.edu
20-Aug-1999 12:25
It would ALSO be extremely valuable to be able to get the RESULTS from an applied function, in the form of an array (especially without changing the original array). IE:

<pre>
function quote( $thing ){ return ' " ' . $thing . ' " '; }
$quoted_array = array_walk( array( 1, "Jack", 6.345 ) );
</pre>



hholzgra@media-engineering.de
15-Dec-1999 01:57
at least with the current CVS version
of PHP4 (Dec.15 1999) the example
above won't work without calling
reset($fruits) before the second and third call of array_walk()

( this behaviour is different than in PHP3 )



niall@kst.com
18-Apr-2000 10:25
If you want to pass another value, then add it to the array (multi-dimensional). No need to ruin a perfectly good function! ;)


 About Notes


Previous page
 array_values
 Updated
Sat, 12 Aug 2000
arsort 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.