Share PHParray_walk () function tips _ PHP Tutorial

Source: Internet
Author: User
This article describes how to use the PHParray_walk () function. The PHParray_walk () function defines and uses the PHParray_walk () function to apply a callback function to each element in the array. If the call succeeds, TRUE is returned. otherwise, FALSE is returned. Funct in typical cases

PHP array_walk () function definition and usage

The PHP 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 ().

PHP array_walk () function syntax

Array_walk (array, function, userdata ...)

PHP array_walk () function parameters and descriptions

Array is required. Specified array.
Required. The name of the user-defined function.
Userdata is optional. The value that you enter can be used as a callback function parameter.

PHP array_walk () function prompts 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 of the PHP array_walk () function

 
  1. < ?php
  2. function myfunction($value,$key)
  3. {
  4. echo "The key $key has the value
    $value
    ";
  5. }
  6. $a=array("a"=>"Cat","b"=>"Dog",
    "c"=>"Horse");
  7. array_walk($a,"myfunction");
  8. ?>

Output:

The key a has the value Cat
The key B has the value Dog
The key c has the value Horse


Http://www.bkjia.com/PHPjc/446073.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446073.htmlTechArticlePHP array_walk () function definition and usage PHP 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, funct...

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.