PHP array_walk_recursive uses a custom function to handle every element in an array _php instance

Source: Internet
Author: User

Array_walk_recursive user functions recursively in each member of an array

Basic syntax

BOOL Array_walk_recursive (array & $input, callable $funcname [, mixed $userdata = NULL])

Applies the user-defined function funcname to each cell in an array. This function recursively goes to a deeper array. In the FuncName function, the key name and key value of the array are the parameters of the function.

Parameter introduction:

Parameters Description
Input Necessary. The array entered.
FuncName Necessary. The name of the user-defined function. Typically, the funcname accepts two parameters. The value of the input parameter is the first, and the key name is the second.
UserData Optional. If an optional parameter userdata is provided, it is passed as the third argument to callback funcname.

Note: If funcname needs to act directly on the values in the array, the first argument to FuncName is specified as a reference. Any changes to these units will also change the original array itself.

return value

Returns TRUE on success or FALSE on failure.

Instance:

<?php
$sweet = Array (
  ' a ' => ' apple ', '
  b ' => ' banana '
);
$fruits = Array (
  ' sweet ' => $sweet,
  ' sour ' => ' lemon '
);
function Test_print ($item, $key) {
  echo $key holds $item <br/>;
}
Array_walk_recursive ($fruits, ' test_print ');

Run Result:

A holds Apple
B holds banana
Sour holds lemon

Thank you for reading, I hope to help you, thank you for your support for this site!

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.