Blogger Information
Blog 59
fans 6
comment 0
visits 51915
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组定义、访问、遍历,指针使用-php17课7.10
希望
Original
768 people have browsed it

一、数组的定义与访问


1. 数组的定义

  • 创建数组 $staff = []
  • 索引数组以追加的方式添加,默认从0开始递增
  • 关联数组,访问键,是字符串,有语义化的,与位置无关,只与键名相关
  • 多维数组,二维用的最多

 数组的定义

  • 代码如下:
  1. <?php
  2. // 创建数组
  3. $staff = [];
  4. // 1.索引数组,以追加的方式添加
  5. $staff[] = '20';
  6. $staff[] = 'abc';
  7. $staff[] = '123@qq.com';
  8. printf('<pre>%s</pre>', print_r($staff, true));
  9. // 2.关联数组
  10. $staff = [];
  11. $staff['id'] = '20';
  12. $staff['name'] = 'abc';
  13. $staff['email'] = '123@qq.com';
  14. printf('<pre>%s</pre>', print_r($staff, true));
  15. // 3.多维数组,二维用的最多
  16. $users = [
  17. ['id' => 1, 'name' => '小明', 'age' => 20],
  18. ['id' => 2, 'name' => '小李', 'age' => 21],
  19. ['id' => 3, 'name' => '小刘', 'age' => 22],
  20. ];
  21. printf('<pre>%s</pre>', print_r($users, true));
  22. // 4.如何拿到小刘?
  23. echo $users[2]['name'];

2. 获取数组与访问

  • 用parse_str查询字符串,只获取内容:id=1&name=admin&role=1
    数组的定义与访问
  • 代码如下:
  1. print_r($_SERVER['QUERY_STRING']);
  2. echo '<hr>';
  3. parse_str($_SERVER['QUERY_STRING'], $queryArr);
  4. print_r($queryArr);
  5. echo '<hr>';
  6. print_r(parse_url('http: //php.edu/0710/demo1.php?id=1&name=admin&role=1'));
  7. echo '<hr>';
  8. print_r(parse_url('http: //php.edu/0710/demo1.php?id=1&name=admin&role=1')['query']);

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


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

  • current():获取当前位置的数组元素的值value
  • key():获取当前key
    数组的遍历使用指针
  • 代码如下:
    1. $stu = ['id' => '01', '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举例来遍历数组,获取值

for,whie,foreach

  • 代码如下:
  1. // 1.用while循环来实现遍历
  2. $stu = ['id' => '01', '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' => '01', 'name' => '小芳', 'age' => 20, 'course' => 'php', 'grade' => 99];
  20. // 用key取键,用value取值
  21. foreach ($stu as $key => $value) {
  22. echo "['{$key}'] =>$value <br>";
  23. }

3. 如果值为数组,共3类课程,如何打印?

  1. $stu = ['id' => '01', 'name' => '小芳', 'age' => 20, 'course' => ['php', 'js', 'css'], 'grade' => 99];
  2. foreach ($stu as $value) {
  3. echo is_array($value) ? print_r($value, true) : $value;
  4. }

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

list

  • 代码如下:
  1. // 1.list():针对索引数组,解析/解构
  2. list($a, $b, $c) = [10, 20, 30];
  3. echo $a, $b, $c;
  4. echo '<hr>';
  5. // 2.list():关联数组,解析/解构
  6. list('lesson' => $lesson, 'grad' => $grad) = ['lesson' => 'html', 'grad' => 90];
  7. echo $lesson, '===>', $grad;
  8. echo '<hr>';
  9. list('id' => $id, 'name' => $name, 'age' => $age) = ['id' => '01', 'name' => '小李', 'age' => 21];
  10. foreach ($users as list('id' => $id, 'name' => $name, 'age' => $age)) {
  11. printf('id=%s, 姓名=%s, 年龄:%s<hr>', $id, $name, $age);
  12. }
Correcting teacher:GuanhuiGuanhui

Correction status:qualified

Teacher's comments:内容有点少!list一定好好利用,可以帮助写出优雅的代码。
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