Blogger Information
Blog 8
fans 0
comment 0
visits 6214
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组的定义与访问, 数组的遍历,使用指针
多喝水
Original
969 people have browsed it

一、数组的定义与访问

1.数组的定义

  • 索引数组:位置敏感,访问严重依赖元素在数组中的位置
  • 索引数组的元素的索引,默认从0开始递增1
  • 关联数组:键的类型是字符串,应该有语义化
  • 关联数组的访问与元素在数组中的位置无关,只与它的键名相关
  • 多维数组,二维用的最多

    代码如下

    1. <?php
    2. $staff = [];
    3. $staff[] = '10';
    4. $staff[] = '八戒';
    5. $staff[] = '123@qq.com';
    6. printf('<pre>%s</pre>',print_r($staff,true));
    7. //关联
    8. $staff = [];
    9. $staff['id'] = '100';
    10. $staff['name'] = '八戒';
    11. $staff['email'] = '123@qq.com';
    12. printf('<pre>%s</pre>',print_r($staff,true));
    13. //多维
    14. $users = [
    15. ['id'=>1,'name'=>'老公','age'=>40],
    16. ['id'=>2,'name'=>'老婆','age'=>30],
    17. ['id'=>3,'name'=>'孩子','age'=>10],
    18. ];
    19. printf('<pre>%s</pre>',print_r($users,true));
    20. echo $users[1]['name'];

    2.. 获取数组与访问

  • 用parse_str查询字符串,只获取内容:id=1&name=admin&role=1
    1. <?php
    2. print_r($_SERVER['QUERY_STRING']);
    3. echo '<hr>';
    4. parse_str($_SERVER['QUERY_STRING'], $queryArr);
    5. print_r($queryArr);
    6. echo '<hr>';
    7. print_r(parse_url('http: //php.edu/0710/demo2.php?id=1&name=admin&role=1'));
    8. echo '<hr>';
    9. print_r(parse_url('http: //php.edu/0710/demo2.php?id=1&name=admin&role=1')['query']);

    二、数组的遍历,使用指针,for/whie,foreach,list的用法

    1. 使用数组指针,逐个遍历


    代码如下:
    1. $stu = ['id' => '1982', 'name' => '梅超风', 'age' => 20, 'course' => 'php', 'grade' => 99];
    2. // 拿键和拿值
    3. printf('[\'%s\']=>%s<br>', key($stu), current($stu));
    4. // 访问完成之后,指针手工后移
    5. next($stu);
    6. printf('[\'%s\']=>%s<br>', key($stu), current($stu));
    7. // 指针前移
    8. prev($stu);
    9. printf('[\'%s\']=>%s<br>', key($stu), current($stu));
    10. // 直接访问最后一个
    11. end($stu);
    12. printf('[\'%s\']=>%s<br>', key($stu), current($stu));
    13. // 指针复位,回到第一个
    14. reset($stu);
    15. printf('[\'%s\']=>%s<br>', key($stu), current($stu));

    2. while,for,foreach举例来遍历数组,获取值

  1. // 用while循环来实现遍历
  2. $stu = ['id' => '1982', 'name' => '梅超风', 'age' => 20, 'course' => 'php', 'grade' => 99];
  3. reset($stu);
  4. while (true) {
  5. printf('[\'%s\']=>%s<br>', key($stu), current($stu));
  6. if (next($stu)) continue;
  7. else break;
  8. }
  9. echo '<hr>';
  10. // 2.用for循环来实现遍历关联数组
  11. reset($stu);
  12. for ($i = 0; $i < count($stu); $i++) {
  13. printf('[\'%s\']=>%s<br>', key($stu), current($stu));
  14. next($stu);
  15. }
  16. echo '<hr>';
  17. // 3.foreach循环来实现遍历关联数组,推荐使用
  18. // 不用指针复位;不用手工移动,自动后移
  19. $stu = ['id' => '1982', 'name' => '梅超风', 'age' => 20, 'course' => 'php', 'grade' => 99];
  20. // 用key取键,用value取值
  21. foreach ($stu as $key => $value) {
  22. echo "['{$key}'] =>$value <br>";
  23. }

3.list():针对索引数组和关联数组,解析/解构


代码如下

  1. //list()解构/解析:索引数组
  2. list($a,$b,$c) = [10,30,50];
  3. echo $a,$b,$c;
  4. echo '<hr>';
  5. //list()解构/解析:关联数组
  6. list('lesson'=>$lesson,'grade'=>$grade) = ['lesson'=>'js','grade'=>80];
  7. echo $lesson,'===>',$grade;
Correcting teacher:GuanhuiGuanhui

Correction status:qualified

Teacher's comments:while,for,foreach 都可以历遍数组,但是都有不同的使用场景,一定要合理使用。
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
Author's latest blog post