PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




array_walk

Name

array_walk — Applies a specified function to each element of an array, along with optional input.

Synopsis

bool array_walk(array, func, userdata);
array array: Array over which to iterate
string func: Function name to apply to array elements
mixed userdata:

Returns

TRUE on success; NULL if given invalid arguments

Description

Traverses the given array and passes each element into the given function in turn. The function given as func is called once for every element of array . The function named by func can accept up to three parameters for each element:

  • param1 : Key of the current element

  • param2 : Value of the current element

  • param3 : Value of the userdata argument to array_walk() , if given



If the function named by func accepts more than three parameters, it generates a warning, unless that warning is suppressed either by using the error_reporting() function or by prepending the @ error-suppression operator to the array_walk() call.

array_walk() doesn't directly alter the elements of array unless you explicitly tell it to do so, by writing the function named by func to take its first parameter by reference.

Version

PHP 3 since 3.0.3, PHP 4

Example

Example 38. Apply a callback function to each element of an array

function output_element($a) {
   print("Element value: $a\n");
}

$arr1 = array("alpha", "baker", "charlie", "delta");
array_walk($arr1, "output_element");

Output:
Element value: alpha
Element value: baker
Element value: charlie
Element value: delta



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.