Blogger Information
Blog 23
fans 1
comment 0
visits 18542
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php-第三课-数组创建及循环-九期线上班
王玉龙℡¹³⁵⁵²⁰⁶²¹³⁹
Original
702 people have browsed it

一、数组创建

数组共有两种创建方法。

1、array创建

<? php

    $arr1 = array( );

?>

image.png

2、方括号创建

<?php

    $arr2 = [ ];

?>

image.png

二、创建索引数组

数组里面的键值对,只给value值,不给key值,key值为默认。

<? php

    $arr = [

        1,

        1.5,

        'php',

        null,

    ]

?>

image.png

print _r( $arr );   返回值

image.png

image.png

三、创建关联数组

<?php  

    $arr = [

         1 => 'php',

         2 => 'html',

        3 => 'css',

        4 => 'js',

    ];

?>

image.png

image.png

image.png

四、访问数组数据

数组数据用 print_r(变量名[key]访问,key可以多层访问

<?php

$arr = [

    1 => 'php',

    2 => 'html',

    3 => 'css',

    4 => 'js',

]

访问数组 print_r( $arr变量名);

访问键值对 print_r( $arr变量名[1]下标);

?>

image.png

键值对访问结果

image.png

五、创建二维数组

<?php

    $arr = [

        1 =>  [

            'qianduan1' => 'html',

            'qianduan2'  => 'css',

            'qianduan3' => 'js',

        ],

        2 =>  [            

            'houduan1' => 'php',
             'houdua2' => 'mysql',

            'houduan3' => '数据库',

            ]

];

?>

image.png

打印值:

image.png

image.png

六、三维数组

<?php

        $arr = [

                        [

                            'name' => '李小龙',

                            'age' => '60',

                            'daibiaozuo' =>[

                                                            '猛龙过江’,

                                                         ‘精武门’,

                                                        ‘龙争虎斗’

                                                       ]

                            ],

                          [
                            'name' => '杰森斯坦森',
                            'age' => '50',
                            'daibiaozuo' =>[
                                                            '速度与激情’,
                                                         ‘机械师’,                                                        ‘

                                                            ’敢死队‘,

                                                       ]
                            ],

                            ];

打印“机械师”

print_r($arr[1]['daibiaozuo'][1]);

?>

image.png

image.png

七、二维数组循环

$arr = [
   [
       'name' => '李小龙',
       'age' => '60',
       'dianying' => '猛龙过江',
   ],
   [
       'name' => '杰森斯坦森',
       'age' => '50',
       'dianying'=> '敢死队'
   ]
];

循环'name'

foreach ($arr as $aa){

   print_r($aa[name]);
   echo '<hr>';

循环结果:

image.png

image.png

image.png

8、三维数组循环

$arr = [
   [
       'name' => '李小龙',
       'age' => '60',
       'dianying' =>[
           '猛龙过江',
           '精武门',
           '龙争虎斗',
                   ]
   ],
       [
       'name' => '杰森斯坦森',
       'age' => '50',
       'dianying' =>[
           '速度与激情',
           '机械师',
           '敢死队'
                   ]
   ]
       ];

打印结果:精武门、机械师。
foreach ($arr as $aa){
   foreach ($aa as $nn);
       print_r($nn[1]);
   echo '<br>';

image.png

image.png

image.png

Correcting teacher:查无此人查无此人

Correction status:qualified

Teacher's comments:完成的不错。
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments