Rumah > pembangunan bahagian belakang > tutorial php > Android程序员学PHP开发(20)-数组(2)多维数组和实际应用-PhpStorm

Android程序员学PHP开发(20)-数组(2)多维数组和实际应用-PhpStorm

黄舟
Lepaskan: 2023-03-06 10:16:02
asal
1151 orang telah melayarinya

在PHP开发中,有很大部分比例,我们是在操作数组,所以数组的学习非常关键~~

<?php
    /**
     * 二维数组
     */
    $group = array(
        array("name"=>"iwanghang", "age"=>18, "sex"=>"男", "email"=>"iwanghang@qq.com"),  // $group[0]
        array("name"=>"queen", "age"=>14, "sex"=>"女", "email"=>"queen@qq.com"), // $group[1]
        array("name"=>"king", "age"=>55, "sex"=>"男", "email"=>"king@qq.com"), // $group[2]
        "four"=>array("name"=>"xiaoming", "age"=>8, "sex"=>"男", "email"=>"xiaoming@qq.com")
    );
    var_dump($group[1]); // 打印结果 : array(4) { ["name"]=> string(5) "queen" ["age"]=> int(14)
                         // ["sex"]=> string(3) "女" ["email"]=> string(12) "queen@qq.com" }
    echo "<br>";
    var_dump($group[1]["name"]); // 打印结果 : string(5) "queen"
    echo "<br>";
    var_dump($group[2]["age"]); // 打印结果 : int(55)
    echo "<br>";
    var_dump($group["four"]["email"]); // 打印结果 : string(15) "xiaoming@qq.com"
    echo "<br>";
Salin selepas log masuk


<?php
/**
 * 多维数组
 */
    $class = [
                "group1"=>[
                    ["name"=>"iwanghang", "age"=>18, "sex"=>"男", "email"=>"iwanghang@qq.com"],  // $group[0]
                    ["name"=>"queen", "age"=>14, "sex"=>"女", "email"=>"queen@qq.com"], // $group[1]
                    ["name"=>"king", "age"=>55, "sex"=>"男", "email"=>"king@qq.com"], // $group[2]
                ],
                [
                    ["name"=>"iwanghang", "age"=>18, "sex"=>"男", "email"=>"iwanghang@qq.com"],  // $group[0]
                    ["name"=>"queen", "age"=>14, "sex"=>"女", "email"=>"queen@qq.com"], // $group[1]
                    ["name"=>"king", "age"=>55, "sex"=>"男", "email"=>"king@qq.com"], // $group[2]
                ],
                [
                    ["name"=>"iwanghang", "age"=>18, "sex"=>"男", "email"=>"iwanghang@qq.com"],  // $group[0]
                    ["name"=>"queen", "age"=>14, "sex"=>"女", "email"=>"queen@qq.com"], // $group[1]
                    ["name"=>"king", "age"=>55, "sex"=>"男", "email"=>"king@qq.com"], // $group[2]
                ]
    ];
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    echo &#39;---------- $class ----------<br>&#39;;
    print_r($class);
    echo &#39;---------- $class[0] ----------<br>&#39;;
    print_r($class[0]);
    echo &#39;---------- $class[0][0] ----------<br>&#39;;
    print_r($class[0][0]);
    echo &#39;---------- $class[0][0][\&#39;name\&#39;] ----------<br>&#39;;
    print_r($class[0][0][&#39;name&#39;]);
    echo &#39;<br>----------- $class["group1"][0][\&#39;name\&#39;]) -----------<br>&#39;;
    print_r($class["group1"][0][&#39;name&#39;]);

    /**
     * 打印结果:
            ---------- $class ----------
            Array
            (
                [group1] => Array
                    (
                        [0] => Array
                            (
                                [name] => iwanghang
                                [age] => 18
                                [sex] => 男
                                [email] => iwanghang@qq.com
                            )

                        [1] => Array
                            (
                                [name] => queen
                                [age] => 14
                                [sex] => 女
                                [email] => queen@qq.com
                            )

                        [2] => Array
                            (
                                [name] => king
                                [age] => 55
                                [sex] => 男
                                [email] => king@qq.com
                            )

                    )

                [0] => Array
                    (
                        [0] => Array
                            (
                                [name] => iwanghang
                                [age] => 18
                                [sex] => 男
                                [email] => iwanghang@qq.com
                            )

                        [1] => Array
                            (
                                [name] => queen
                                [age] => 14
                                [sex] => 女
                                [email] => queen@qq.com
                            )

                        [2] => Array
                            (
                                [name] => king
                                [age] => 55
                                [sex] => 男
                                [email] => king@qq.com
                            )

                    )

                [1] => Array
                    (
                        [0] => Array
                            (
                                [name] => iwanghang
                                [age] => 18
                                [sex] => 男
                                [email] => iwanghang@qq.com
                            )

                        [1] => Array
                            (
                                [name] => queen
                                [age] => 14
                                [sex] => 女
                                [email] => queen@qq.com
                            )

                        [2] => Array
                            (
                                [name] => king
                                [age] => 55
                                [sex] => 男
                                [email] => king@qq.com
                            )

                    )

            )
            ---------- $class[0] ----------
            Array
            (
                [0] => Array
                    (
                        [name] => iwanghang
                        [age] => 18
                        [sex] => 男
                        [email] => iwanghang@qq.com
                    )

                [1] => Array
                    (
                        [name] => queen
                        [age] => 14
                        [sex] => 女
                        [email] => queen@qq.com
                    )

                [2] => Array
                    (
                        [name] => king
                        [age] => 55
                        [sex] => 男
                        [email] => king@qq.com
                    )

            )
            ---------- $class[0][0] ----------
            Array
            (
                [name] => iwanghang
                [age] => 18
                [sex] => 男
                [email] => iwanghang@qq.com
            )
            ---------- $class[0][0][&#39;name&#39;] ----------
            iwanghang
            ----------- $class["group1"][0][&#39;name&#39;]) -----------
            iwanghang
     */
Salin selepas log masuk


<?php
    /**
     * 销毁指定下标Demo
     * isset 检测变量是否存在
     * unset 销毁指定的变量
     */
    echo &#39;---------- $arr = ["one", "two", "three". "four"]; ----------<br>&#39;;
    $arr = ["one", "two", "three", "four"];

    echo &#39;---------- isset($arr[2]) ----------<br>&#39;;
    if (isset($arr[2])){ // 打印结果:存在
        echo "存在";
    }else{
        echo "不存在";
    }
    echo "<br>";

    echo &#39;---------- print_r($arr) ----------<br>&#39;;
    print_r($arr); // 打印结果:Array ( [0] => one [1] => two [2] => three [3] => four )
    echo "<br>";

    echo &#39;---------- unset($arr[2]) ----------<br>&#39;;
    unset($arr[2]);
    if (isset($arr[2])){ // 打印结果:不存在
    echo "存在";
    }else{
        echo "不存在";
    }
    echo "<br>";

    echo &#39;---------- print_r($arr) ----------<br>&#39;;
    print_r($arr); // 打印结果:Array ( [0] => one [1] => two [3] => four )
    echo "<br>";

    echo &#39;---------- $arr[2] = null ----------<br>&#39;;
    $arr[2] = null;
    if (isset($arr[2])){ // 打印结果:不存在
        echo "存在";
    }else{
        echo "不存在";
    }
    echo "<br>";

    echo &#39;---------- print_r($arr) ----------<br>&#39;;
    print_r($arr); // 打印结果:Array ( [0] => one [1] => two [3] => four [2] => )
    echo "<br>";
Salin selepas log masuk


<?php
    /**
     * 猴子选大王
     * xdw 选大王
     * count($arr) 数组下标
     */

    xdw(88,5); // 30只猴子,每3只出局1只

    function xdw($m, $n){
        $arr = array();

        $a = "a";

        for ($i=0; $i<$m; $i++){
            $arr[] = $a++;
        }

        print_r($arr);

        $i = 0;
        while (count($arr)>1){
            if ($i % $n == 0){
                echo $i."除以".$n."余数为0,所以删除".$arr[$i]."<br>";
                unset($arr[$i]);
            }else{
                $arr[] = $arr[$i];
                print_r($arr);
                echo "<br>";
                unset($arr[$i]);
            }
            $i++;
        }

        print_r($arr);
    }
Salin selepas log masuk

以上就是Android程序员学PHP开发(20)-数组(2)多维数组和实际应用-PhpStorm的内容,更多相关内容请关注PHP中文网(www.php.cn)!



sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan