PHP的多维数组定义以及使用for循环遍历数组

    $array=[
        "市场部"=>[
                 ["name"=>"经理","sex"=>"man","age"=>22,"email"=>"[email protected]"],
                 ["name"=>"ZS","sex"=>"woman","age"=>20,"email"=>"[email protected]"],
                 ["name"=>"LS","sex"=>"man","age"=>24,"email"=>"[email protected]"]
                ],
        "外贸部"=>[
                 ["name"=>"经理","sex"=>"man","age"=>28,"email"=>"[email protected]"],
                 ["name"=>"ZS","sex"=>"woman","age"=>30,"email"=>"[email protected]"],
                 ["name"=>"LS","sex"=>"man","age"=>34,"email"=>"[email protected]"],
                ],
        "财务部"=>[
                 ["name"=>"经理","sex"=>"man","age"=>28,"email"=>"[email protected]"],
                 ["name"=>"ZS","sex"=>"woman","age"=>30,"email"=>"[email protected]"],
                 ["name"=>"LS","sex"=>"man","age"=>34,"email"=>"[email protected]"]
                ],
            ];
    //访问三维数组的方式        
    echo $array["外贸部"][2]["email"];
    echo "
";
    print_r($array["财务部"][0]);
    echo "
";
    
    //直接声明二维数组
    $arr[][]="zs";
    $arr[][]="ls";
    $arr[][]="ww";
    $arr[][]="zl";
    echo "
";
    print_r($arr);//输出时先是外层数组索引值递加
    echo "
";
    
    /*使用for循环遍历数组
    优点:1.执行效率高
    不足:1.只能适用于下标连续的索引数组;
          2.不能遍历关联数组
    */

    $arr=array("");
    $value='a';
    //使用for循环为$arr数组每个元素赋值
    for($a=0;$a<20;$a++)
    {
        $arr[$a]=$value++;
    }
    var_dump($arr);
    //程序执行效率的优化:将count()函数拿出来赋给一个变量,这样就不用每次都在数组中执行系统函数浪费系统资源
    $num=count($arr);
    for($i=0;$i<$num;$i++)
    {
        echo $arr[$i]."
";
    }
?>

你可能感兴趣的:(PHP地基,小程序)