How to use functions such as Array_map () in the PHP framework

Source: Internet
Author: User
    • There will always be some more special needs, and this function is found.

    • The following is excerpted from the Official Handbook Array_map ()

      • Callback--callback function, applied to each element in each array.

      • Array1--Array, traversing the run callback function.

      • Array list, each of which traverses the run callback function.

      • Array_map--Apply a callback function for each element of an array

      • Array Array_map (callable $callback, array $array 1 [, Array $ ...]

      • Array_map (): Returns an array that is after the callback function is applied to each element of array1. The number of callback function parameters and the number of arrays passed to the Array_map () must be the same.

      • Parameters

      • Return value-Returns an array containing all the elements array1 after the callback function has been processed.

      • Example


<?php    $arr = [        [' a ' = = ' AA ', ' b ' = = ' BB ',],        [' c ' = ' cc ', ' d ' = ' dd ',],        [' e ' = ' ee ', ' f ' = ' ff ',],    ;    function test ($v) {        $v [' add '] = 0;        return $v;    }    $arr = Array_map ("Test", $arr);    Print_r ($arr);? >

Output results


Array (    [0] = = Array        (            [A] = AA            [b] = BB            [Add] = 0        )    [1] = = array< c18/> (            [c] = cc            [d] = DD            [Add] = 0        )    [2] = = Array        (            [E] = ee            [f] = = FF            [Add] + 0        ))
    • Framework (thinkphp) example

<?phpnamespace User\controller;use Common\controller\managercontroller;                Class Datacontroller extends controller{public Function get_data () {$arr = [//Data fill];        $arr = Array_map ([$this, ' _add_param '], $arr);    Dump ($arr);        } Private Function _add_param ($value) {$value [' add '] = ' xxx ';    return $value; }}

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.