Blogger Information
Blog 48
fans 3
comment 1
visits 30308
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示php数组的常用函数
吴长清
Original
358 people have browsed it

1.常用函数(课外)

1.1 array_rand():从数组中随机取出一个或多个随机键

  1. $arr = ['id' => 1, 'name' => '赵大', 'email' => 'zhaoda@qq.com', 'hello', 'php', 'js'];
  2. printf('原数组: <pre>%s</pre>', print_r($arr, true));
  3. echo '<hr>';
  4. // 1.array_rand(数组,要取出的单元数量): 从数组中随机取出一个或多个随机键
  5. printf('1.随机取出键名: <pre>%s</pre>', print_r(array_rand($arr, 3),true));

1.2 array_flip():交换数组中的键和值

  1. $arr = ['id' => 1, 'name' => '赵大', 'email' => 'zhaoda@qq.com', 'hello', 'php', 'js'];
  2. printf('原数组: <pre>%s</pre>', print_r($arr, true));
  3. echo '<hr>';
  4. // 2.array_flip(数组)
  5. printf('2.交换数组中的键和值: <pre>%s</pre>', print_r(array_flip($arr),true));

1.3 array_diff():计算数组的差集

  1. // 3.array_diff(数组): 计算数组的差集
  2. $array1 = array("a" => "green", "red", "blue", "red");
  3. $array2 = array("b" => "green", "yellow", "red");
  4. printf('数组1: <pre>%s</pre>', print_r($array1,true));
  5. printf('数组2: <pre>%s</pre>', print_r($array2,true));
  6. // 数组$array1中的值与数组$array2中的值做对比,返回$array2中没有$array1中的值
  7. $result = array_diff($array1, $array2);
  8. printf('数组1与数组2的差集: <pre>%s</pre>', print_r($result,true));

1.4 array_combine(): 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值

  1. //4.array_combine(): 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
  2. $arr1 = ['id', 'name', 'email'];
  3. $arr2 = [1, '赵大', 'zhaoda@qq.com'];
  4. printf('数组1: <pre>%s</pre><hr>', print_r($arr1, true));
  5. printf('数组2: <pre>%s</pre><hr>', print_r($arr2, true));
  6. $result = array_combine($arr1, $arr2);
  7. printf('用一个数组的值作为其键名,另一个数组的值作为其值: <pre>%s</pre>', print_r($result, true));

1.5 array_intersect(): 计算数组的交集

  1. // 5.array_intersect(): 计算数组的交集
  2. $array1 = array("a" => "green", "red", "blue", "red");
  3. $array2 = array("b" => "green", "yellow", "red");
  4. printf('数组1: <pre>%s</pre>', print_r($array1,true));
  5. printf('数组2: <pre>%s</pre>', print_r($array2,true));
  6. $result = array_intersect($array1, $array2);
  7. printf('数组1与数组2的交集: <pre>%s</pre>', print_r($result,true));

1.6 array_reverse(): 返回单元顺序相反的数组

  1. // 6.array_reverse(): 返回单元顺序相反的数组
  2. $arr = ['id' => 1, 'name' => '赵大', 'email' => 'zhaoda@qq.com', 'hello', 'php', 'js'];
  3. printf('原数组: <pre>%s</pre> <hr>', print_r($arr, true));
  4. printf('取反数组: <pre>%s</pre> <hr>', print_r(array_reverse($arr), true));

2.队列

  1. <body>
  2. <fieldset class="list">
  3. <legend>尾部追加 头部删除</legend>
  4. <?php $arr = [1, 2, 3, 4, 5];
  5. foreach ($arr as $value) : ?>
  6. <div class="box"> <?= $value ?></div>
  7. <?php endforeach ?>
  8. <?php
  9. echo "<hr>";
  10. array_push($arr, $arr[count($arr) - 1] + 1);
  11. array_shift($arr);
  12. ?>
  13. <?php foreach ($arr as $value) : ?>
  14. <div class="box"> <?= $value ?></div>
  15. <?php endforeach ?>
  16. <?php
  17. echo "<hr>";
  18. array_push($arr, $arr[count($arr) - 1] + 1);
  19. array_shift($arr);
  20. ?>
  21. <?php foreach ($arr as $value) : ?>
  22. <div class="box"> <?= $value ?></div>
  23. <?php endforeach ?>
  24. <?php
  25. echo "<hr>";
  26. array_push($arr, $arr[count($arr) - 1] + 1);
  27. array_shift($arr);
  28. ?>
  29. <?php foreach ($arr as $value) : ?>
  30. <div class="box"> <?= $value ?></div>
  31. <?php endforeach ?>
  32. </fieldset>
  33. <fieldset class="list">
  34. <legend>头部追加 尾部删除</legend>
  35. <?php $arr = [1, 2, 3, 4, 5];
  36. foreach ($arr as $value) : ?>
  37. <div class="box2"> <?= $value ?></div>
  38. <?php endforeach ?>
  39. <?php
  40. echo "<hr>";
  41. array_unshift($arr, $arr[0] - 1);
  42. array_pop($arr);
  43. ?>
  44. <?php foreach ($arr as $value) : ?>
  45. <div class="box2"> <?= $value ?></div>
  46. <?php endforeach ?>
  47. <?php
  48. echo "<hr>";
  49. array_unshift($arr, $arr[0] - 1);
  50. array_pop($arr);
  51. ?>
  52. <?php foreach ($arr as $value) : ?>
  53. <div class="box2"> <?= $value ?></div>
  54. <?php endforeach ?>
  55. <?php
  56. echo "<hr>";
  57. array_unshift($arr, $arr[0] - 1);
  58. array_pop($arr);
  59. ?>
  60. <?php foreach ($arr as $value) : ?>
  61. <div class="box2"> <?= $value ?></div>
  62. <?php endforeach ?>
  63. </fieldset>
  64. </body>

Correcting teacher:PHPzPHPz

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
Author's latest blog post