PHP--------Recursive functions

Source: Internet
Author: User

Array_walk has a native recursive function array_walk_recursive ($arr, ' function ', ' words '), but Array_map does not have a corresponding recursive function array_map_recursive,

We can customize one:

functionArray_map_recursive ($filter,$data) {    $result=Array(); foreach($data  as $key=$val)     {        $result[$key] =Is_array($val)         ? Array_map_recursive ($filter,$val)         :Call_user_func($filter,$val); }     return $result;}

Application Examples:

$arr=Array(    ' A ' = ' 2 ', ' B ' + =Array(        ' CC ' = ' wen ', ' dd ' = ' jian ', ' ee ' = ' bao '    )); functionFunc_a ($value){    $value. = ' _aaa '; return $value;} Var_dump($arr); $arr= Array_map_recursive (' func_a ',$arr); Var_dump($arr);

In fact, it is entirely possible to implement this function with the array_walk_recursive () function, as shown in the following code:

  $arr  = array   ( ' a ' = ' 2 ', ' b ' = '  function  func_b (& $value    $value . = ' _aaa '  var_dump  ( $arr   array_walk_recursive  ( $arr , ' func _b '  var_dump  ( $arr ); 

Output:

Array (size=2)   string ' 2 ' (length=1)  ' b ' = =     array (size=3      )string ' Wen ' (length=3 String ' Jian ' (length=4)      string ' Bao ' (length=3)       

Array (size=2)   string ' 2_aaa ' (length=5)  ' b ' = =     array (size=3      )string ' wen_aaa ' ( Length=7)      string ' jian_aaa ' (length=8)      string ' bao_aaa ' ( LENGTH=7)

PHP--------Recursive functions

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.