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

PHP Home Page

Manual Table of Contents
Up to Arrays
Quick Reference
English version of this pageGerman version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Arrays
* array
* array_count_values
* array_diff
* array_flip
* array_intersect
* 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 --  配列の各メンバーにユーザー関数を適用する。

説明

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

arrの各要素にfunc という名前の関数を適用します。 funcには、配列の値が最初の引数として 渡され、配列のキーが2番目のパラメータとして渡されます。 userdataが指定された場合、3番目の引数として ユーザー関数に渡されます。 funcが2つまたは3つを超える引数を必要とする場合、 userdataの指定によっては、 array_walk()func をコールする度に警告が生成されます。 これらの警告は、array_walk()の前に '@'マークを付けるか error_reporting()を使用することにより 抑制されます。

注意: funcにより配列の値そのものを変更する必要 がある場合、funcの最初の引数は参照として 渡す必要があります。この場合、配列の要素に加えた変更は、配列自体 に対して行われます。

注意: キー及びuserdataをfuncに渡す処理は、 バージョン 4.0で追加されました。

PHP 4では、array_walk()で配列のリセットがデフォ ルトで行われないため、reset()をコールする必要 があります。

例 1. array_walk()の例


$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');
reset ($fruits);
array_walk ($fruits, 'test_alter', 'fruit');
reset ($fruits);
array_walk ($fruits, 'test_print');
      

each()および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.