Home  >  Article  >  Backend Development  >  Android programmers learn PHP development (24) - Array operation related functions (2) Callback functions - PhpStorm

Android programmers learn PHP development (24) - Array operation related functions (2) Callback functions - PhpStorm

黄舟
黄舟Original
2017-03-03 09:48:05946browse

This article mainly uses callback functions to process arrays, 2 ways:

Function call, the function structure is clear at a glance

Anonymous function, instant use, high security

The following demonstrates the use of several functions: mainly to understand the use of callback functions:

count () Calculate the number of cells in the array or the number of attributes in the object

array_count_values() Count the number of occurrences of all values ​​in the array

array_unique() Remove duplicate values ​​in the array

array_filter() Use the callback function to filter the cells in the array

array_walk() Use The user-defined function performs callback processing on each element in the array

array_map() applies the callback function to each element in the array

Through array_filter() and array_walk(), focus on the use of callback functions, especially anonymous functions with higher security

<?php
    /**
     * 数组的相关处理函数:
     * http://www.php.cn/
     *
     * 下面演示几个函数的使用:主要是了解回调函数的使用
     * count() 计算数组中的单元数目或对象中的属性个数
     * array_count_values() 统计数组中所有的值出现的次数
     * array_unique() 移除数组中重复的值
     * array_filter() 用回调函数过滤数组中的单元
     * array_walk() 使用用户自定义函数对数组中的每个元素做回调处理
     * array_map() 为数组的每个元素应用回调函数
     * 通过array_filter()和array_walk()重点了解一下回调函数的使用,尤其是安全性较高的匿名函数
     */
    $lamp = array("os"=>"Linux", "webserver"=>"Apache", "db"=>"MySQL", "language"=>"PHP");

    echo &#39;---------- print_r() ----------<br>&#39;;
    print_r($lamp); // 打印结果:Array ( [os] => Linux [webserver] => Apache [db] => MySQL [language] => PHP )
    echo &#39;<br>&#39;;

    /**
     * count() 计算数组中的单元数目或对象中的属性个数
     */
    echo &#39;---------- count() 字符串 ----------<br>&#39;;
    $str = "hello world";
    $str2 = "";
    var_dump(count($str)); // 打印结果:int(1)
    echo &#39;<br>&#39;;
    var_dump(count($str2)); // 打印结果:int(1)
    echo &#39;<br>&#39;;

    echo &#39;---------- count() 数组 ----------<br>&#39;;
    echo count($lamp); // 打印结果:4
    echo &#39;<br>&#39;;

    echo &#39;---------- count() 多维数组 ----------<br>&#39;;
    $web = array(
            "lamp"=>array("os"=>"Linux", "webserver"=>"Apache", "db"=>"MySQL", "language"=>"PHP"),
            "lamp2"=>array("os"=>"Linux", "webserver"=>"Apache", "db"=>"MySQL", "language"=>"PHP"),
            "lamp3"=>array("os"=>"Linux", "webserver"=>"Apache", "db"=>"MySQL", "language"=>"PHP")
        );
    echo count($web); // 打印结果:3
    echo &#39;<br>&#39;;
    echo count($web,1); // 打印结果:15 , 数组有3个元素,子数组有12个元素
    echo &#39;<br>&#39;;

    /**
     * array_count_values() 统计数组中所有的值出现的次数
     */
    echo &#39;---------- array_count_values() ----------<br>&#39;;
    $lamp4 = array("os"=>"Linux", "webserver"=>"Apache", "db"=>"MySQL", "db2"=>"MySQL", "language"=>"PHP");
    echo print_r(array_count_values($lamp4)); // 打印结果:Array ( [Linux] => 1 [Apache] => 1 [MySQL] => 2 [PHP] => 1 ) 1
    echo &#39;<br>&#39;;

    /**
     * array_unique() 移除数组中重复的值
     * 第一次出现的value保留其key,其他的移除
     */
    echo &#39;---------- array_unique() ----------<br>&#39;;
    $lamp5 = array("os"=>"Linux", "webserver"=>"Apache", "db"=>"MySQL", "db2"=>"MySQL", "language"=>"PHP");
    echo print_r(array_unique($lamp5)); // 打印结果:Array ( [os] => Linux [webserver] => Apache [db] => MySQL [language] => PHP ) 1
    echo &#39;<br>&#39;;

    /**
     * array_filter() 用回调函数过滤数组中的单元
     */
    echo &#39;---------- array_filter() ----------<br>&#39;;
    $arr = array(1,2,false,-3,null,-2,3,4,"",5,-5,-4,-1);
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    var_dump($arr);
    /*打印结果:
            array(13) {
              [0]=>
              int(1)
              [1]=>
              int(2)
              [2]=>
              bool(false)
              [3]=>
              int(-3)
              [4]=>
              NULL
              [5]=>
              int(-2)
              [6]=>
              int(3)
              [7]=>
              int(4)
              [8]=>
              string(0) ""
              [9]=>
              int(5)
              [10]=>
              int(-5)
              [11]=>
              int(-4)
              [12]=>
              int(-1)
            }
    */
    echo &#39;---------- array_filter() 不传参调用----------<br>&#39;;
    var_dump(array_filter($arr));
    /*打印结果:结果中,false、null、"",被过滤掉了
            array(10) {
              [0]=>
              int(1)
              [1]=>
              int(2)
              [3]=>
              int(-3)
              [5]=>
              int(-2)
              [6]=>
              int(3)
              [7]=>
              int(4)
              [9]=>
              int(5)
              [10]=>
              int(-5)
              [11]=>
              int(-4)
              [12]=>
              int(-1)
            }
      */
    echo &#39;---------- array_filter() 通过自定义函数过滤 ----------<br>&#39;;
    function myfun($value){ // 自己写的函数,大于等于0返回真,否则返回假
        if ($value>=0){
            return true;
        }else{
            return false;
        }
    }
    var_dump(array_filter($arr,"myfun"));
    /*打印结果:结果中,小于0的,被过滤掉了
            array(8) {
              [0]=>
              int(1)
              [1]=>
              int(2)
              [2]=>
              bool(false)
              [4]=>
              NULL
              [6]=>
              int(3)
              [7]=>
              int(4)
              [8]=>
              string(0) ""
              [9]=>
              int(5)
            }
    */
    echo &#39;---------- array_filter() 通过匿名函数过滤 ----------<br>&#39;;
    echo &#39;---------- 当其他地方不需要调用这个函数,推荐使用匿名函数,方便且安全 ----------<br>&#39;;
    var_dump(array_filter($arr, function ($value){
        return !($value%2==0);
    }));
    /*打印结果:结果中,2的倍数,被过滤掉了
            array(6) {
              [0]=>
              int(1)
              [3]=>
              int(-3)
              [6]=>
              int(3)
              [9]=>
              int(5)
              [10]=>
              int(-5)
              [12]=>
              int(-1)
            }
    */
    /**
     * array_walk() 使用用户自定义函数对数组中的每个元素做回调处理
     */
    echo &#39;---------- array_walk() ----------<br>&#39;;
    $arr2 = array(1,2,3,4,5);
    print_r($arr2);
    echo &#39;<br>&#39;;
    /*打印结果:数组原型打印
            Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
                [3] => 4
                [4] => 5
            )
    */

    echo &#39;---------- array_walk() 没有引用 $value 调用函数 ----------<br>&#39;;
    function myfun2($value){ // 没有引用 $value
        $value=$value*$value;
    }
    array_walk($arr2,"myfun2"); // 没有引用 $value
    print_r($arr2);
    echo &#39;<br>&#39;;
    /*打印结果:
            Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
                [3] => 4
                [4] => 5
            )
    */

    echo &#39;---------- array_walk() 有引用 &$value 调用函数 ----------<br>&#39;;
    function myfun3(&$value){ // 引用 &$value
        $value=$value*$value;
    }
    array_walk($arr2,"myfun3"); // 引用 &$value
    print_r($arr2);
    echo &#39;<br>&#39;;
    /*打印结果:
            Array
            (
                [0] => 1
                [1] => 4
                [2] => 9
                [3] => 16
                [4] => 25
            )
    */

    echo &#39;---------- array_walk() 同时处理key和value ----------<br>&#39;;
    $arr3 = array("one"=>1, "two"=>2, "three"=>3, "four"=>4, "five"=>5);
    print_r($arr3);
    echo &#39;<br>&#39;;
    /*打印结果:数组原型打印
            Array
            (
                [one] => 1
                [two] => 2
                [three] => 3
                [four] => 4
                [five] => 5
            )
    */

    echo &#39;---------- array_walk() 调用函数有2个参数 即,同时对key和value操作 ----------<br>&#39;;
    function myfun4(&$value,$key){
        $value=$value*$value;
        echo $key."~~~~~".$value."<br>";
    }
    array_walk($arr3,"myfun4");
    /*打印结果:
            one~~~~~1
            two~~~~~4
            three~~~~~9
            four~~~~~16
            five~~~~~25
    */
    echo &#39;---------- array_walk() 调用函数有2个参数 即,同时对key和value操作 匿名函数----------<br>&#39;;
    array_walk($arr3,function ($value, $key){
        echo $key."~~~~~".$value."<br>";
    });
    /*打印结果:
            one~~~~~1
            two~~~~~4
            three~~~~~9
            four~~~~~16
            five~~~~~25
    */

    /**
     * array_map() 为数组的每个元素应用回调函数
     */
    echo &#39;---------- array_map() ----------<br>&#39;;
    $arr4 = array(1,2,3,4,5);
    $arr5 = array(6,7,8,9,0);
    print_r($arr4);
    echo &#39;<br>&#39;;
    /*打印结果:数组原型打印
            Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
                [3] => 4
                [4] => 5
            )
    */

    echo &#39;---------- array_map() 1 ----------<br>&#39;;
    function myfun5($value){
        return $value*$value*$value;
    }
    $arr4new = array_map("myfun5", $arr4);
    print_r($arr4new);
    echo &#39;<br>&#39;;
    /*打印结果:
            Array
            (
                [0] => 1
                [1] => 8
                [2] => 27
                [3] => 64
                [4] => 125
            )
    */

    echo &#39;---------- array_map() 2 注意return ----------<br>&#39;;
    function myfun6($value){
        echo $value*$value*$value."<br>";
    }
    $arr4new = array_map("myfun6", $arr4);
    print_r($arr4new);
    echo &#39;<br>&#39;;
    /*打印结果:数组里的value没了,因为在函数里没有return,所以,使用array_map()注意return
            1
            8
            27
            64
            125
            Array
            (
                [0] =>
                [1] =>
                [2] =>
                [3] =>
                [4] =>
            )
    */

    echo &#39;---------- array_map() 3 ----------<br>&#39;;
    function myfun7($value, $value2){
        echo "$value~~~~~$value2<br>";
        return 1;
    }
    $arr45new = array_map("myfun7", $arr4,$arr5);
    print_r($arr45new);
    echo &#39;<br>&#39;;
    /*打印结果:
            1~~~~~6
            2~~~~~7
            3~~~~~8
            4~~~~~9
            5~~~~~0
            Array
            (
                [0] => 1
                [1] => 1
                [2] => 1
                [3] => 1
                [4] => 1
            )
    */

    echo &#39;---------- array_map() 4 ----------<br>&#39;;
    $arr45new = array_map(null, $arr4,$arr5);
    print_r($arr45new);
    echo &#39;<br>&#39;;
    /*打印结果:
            Array
            (
                [0] => Array
                    (
                        [0] => 1
                        [1] => 6
                    )

                [1] => Array
                    (
                        [0] => 2
                        [1] => 7
                    )

                [2] => Array
                    (
                        [0] => 3
                        [1] => 8
                    )

                [3] => Array
                    (
                        [0] => 4
                        [1] => 9
                    )

                [4] => Array
                    (
                        [0] => 5
                        [1] => 0
                    )

            )
    */

    echo &#39;---------- array_map() 5 ----------<br>&#39;;
    $arr6 = array(1,2,3,4,5);
    $arr7 = array("one","two","three");
    $arr8 = array("aa","bb","cc","dd");
    $arr678new = array_map(null,$arr6,$arr7,$arr8);
    print_r($arr678new);
    echo &#39;<br>&#39;;
    /*打印结果:
            Array
            (
                [0] => Array
                    (
                        [0] => 1
                        [1] => one
                        [2] => aa
                    )

                [1] => Array
                    (
                        [0] => 2
                        [1] => two
                        [2] => bb
                    )

                [2] => Array
                    (
                        [0] => 3
                        [1] => three
                        [2] => cc
                    )

                [3] => Array
                    (
                        [0] => 4
                        [1] =>
                        [2] => dd
                    )

                [4] => Array
                    (
                        [0] => 5
                        [1] =>
                        [2] =>
                    )

            )
    */

The above is about Android programmers learning PHP development (24) - array operation related Function (2) Callback function - PhpStorm content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn