|
|
 |
(PHP 3>= 3.0.3, PHP 4 >= 4.0.0) array_walk -- Použít uživatelskou funkci na všechny prvky pole Popisint array_walk ( array arr, string func, mixed userdata)
Aplikuje funkci func na všechny prvky pole
arr. Funkci func se jako první
argument předá hodnota a jako druhý klíč. Pokud je přítomen argument
userdata, bude uživatelské funkci předán jako
třetí argument.
Pokud func vyžaduje více než dva nebo tři argumenty
(v závislosti na userdata), pro každé volání
func z array_walk() se vygeneruje
varování. Tato varování se dají potlačit přidáním znaku '@' před
volání array_walk() nebo pomocí
error_reporting().
Poznámka:
Pokud func potřebuje pracovat přímo s daným polem,
první argument func se musí předávat odkazem.
Všechny změny těchto hodnot se pak promítnou přímo
v arr.
Poznámka:
Druhý a třetí argument func byly přidány v PHP 4.0.
V PHP 4 je třeba volat podle potřeby reset(), protože
array_walk() sama vstupní pole neresetuje.
Příklad 1. Ukázka 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'); |
|
Viz také: each() a
list().
User Contributed Notes array_walk |
 |
tix@ollisnet.de
09-Apr-2002 10:02 |
|
Found out some interesting code to call a callback function which is a
function of an object. Here's an example code:
class mod {
var $thearray;
function mod( $a ) {
$this->thearray = $a;
}
function cb_modify( &$item, $key ) {
$item = "pref_".$item;
}
function modify() {
array_walk( $this->thearray, array( $this, 'cb_modify' ) );
}
}
$m = new mod(array("one","two","three"));
$m->modify();
print_r($m);
which prints out:
mod Object
(
[thearray] => Array
(
[0] => pref_one
[1] => pref_two
[2] => pref_three
)
)
As you see, the function of the object is called which modifies the array.
Greetings
TiX
|
|
wes-at-marriedguys-dot-com
18-Apr-2002 08:55 |
|
This might be overly-obvious, but when using array_walk(), the function
being called should not return anything.
For example, imagine a function that does a basic clean-up of a form
field:
function clean_up ($field) {
$field = trim($field);
$field = strip_tags($field);
return $field;
}
This works just fine when passing it a variable like a single field, but if
passing it an array, then you need to remove the return line.
function clean_up_array (&$fields_array) {
$fields_array = trim($fields_array);
$fields_array = strip_tags($fields_array);
}
|
|
|
01-May-2002 11:54 |
|
EXAMPLE WHICH WORKS:
function Proc_Default($val,$key,&$erg) {
$erg.= "<option value='".($val)."'>".($val);
}
$array_walk_proc="Proc_Default";
array_walk($cmd,$array_walk_proc,&$erg);
EXAMPLE WHICH DOES NOT WORK:
function Proc_Default($val,$key,&$erg,$param) {
$erg.= "<option value='".($val)."'>".($val);
}
$array_walk_proc="Proc_Default";
array_walk($cmd,$array_walk_proc,&$erg,$param);
|
|
 |
 | |