PHP Array function array_walk () Note _php Tutorial

Source: Internet
Author: User

PHP Array function array_walk () note


Definition and usage

The Array_walk () function applies a callback function to each element in the array. Returns TRUE if successful, otherwise FALSE.

Typically, a function accepts two parameters. the value of the array parameter as the first, the key name as the second. If an optional parameter userdata is provided, it is passed as the third argument to the callback function.

If the function requires more arguments than is given, a e_warning-level error is generated each time Array_walk () invokes the function. These warnings can be suppressed by adding the PHP error operator @ before the Array_walk () call, or by using error_reporting ().

Grammar

Array_walk (Array,function,userdata ...)

Parameters Description
Array Necessary. Specifies the array.
function Necessary. The name of the user-defined function.
UserData Optional. The value entered by the user, which can be used as a parameter to the callback function.

Hints and Notes

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

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

Note: Passing the key name and UserData to the function is a new addition 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 Cat
The key B has the value Dog
The key C has the value Horse

Example 2

With one parameter:

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

Output:

A has the value Cat
B has the value Dog
C has the value Horse

Example 3

Change the value of the array element (note & $value):(this case is more!) )

 
  "Cat", "b" = "Dog", "C" and "Horse"); Array_walk ($a, "myfunction");p Rint_r ($a);

Output:

Array ([A] = Bird [b] = Bird [c] = Bird)

Articles you may be interested in

    • PHP Array function array_map () note
    • PHP generates consecutive numbers (Letters) array function range () analysis, PHP lottery program function
    • PHP finds whether a value exists in the array (In_array (), Array_search (), array_key_exists ())
    • PHP presses elements into the array header (Array_unshift usage)
    • PHP removes null elements from the array (array_filter)
    • How do I delete an element (Unset,array_splice) in a PHP array?
    • Introduction to the set, intersection, and difference functions of arrays in PHP
    • Use PHP function memory_get_usage to get the current PHP memory consumption to achieve program performance optimization

http://www.bkjia.com/PHPjc/894481.html www.bkjia.com true http://www.bkjia.com/PHPjc/894481.html techarticle the PHP array function array_walk () Note defines and uses the Array_walk () function to apply a callback function to each element in the array. Returns TRUE if successful, otherwise FALSE. The typical case ...

  • 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.