PHParray_walk () function

Source: Internet
Author: User
PHParray_walk () function definition and usage

The array_walk () function applies a callback function to each element in the array. If the call succeeds, TRUE is returned. otherwise, FALSE is returned.

In typical cases, a function accepts two parameters. The value of the array parameter is the first and the key name is the second. If the optional parameter userdata is provided, it is passed to the callback function as the third parameter.

If the function requires more parameters than the given parameter, an E_WARNING error is generated every time array_walk () calls the function. These warnings can be suppressed by adding the PHP error operator @ Before the array_walk () call, or error_reporting ().

Syntax
array_walk(array,function,userdata...)
Parameter description
Array Required. Specified array.
Function Required. The name of the user-defined function.
Userdata Optional. The value that you enter can be used as a callback function parameter.

Tips and comments

Tip: You can set one or more parameters for a function.

Note: If the callback function needs to act directly on the values in the array, you can specify the first parameter of the callback function as reference: & $ value. (See Example 3)

Note: passing the key name and userdata to the function is newly added to PHP 4.0.

Example 1

 ";}$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");array_walk($a,"myfunction");?>

Output:

The key a has the value CatThe key b has the value DogThe key c has the value Horse

Example 2

With a parameter:

 ";}$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");array_walk($a,"myfunction","has the value");?>

Output:

a has the value Catb has the value Dogc has the value Horse

Example 3

Change the value of an array element (note & $ value ):

 "Cat","b"=>"Dog","c"=>"Horse");array_walk($a,"myfunction");print_r($a);?>

Output:

Array ( [a] => Bird [b] => Bird [c] => Bird )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.