Blogger Information
Blog 33
fans 0
comment 0
visits 17260
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php数组函数
lucaslwk
Original
403 people have browsed it

php数组函数演示

  1. <?php
  2. $arr = [1, 2, 3, 4, 5];
  3. function printArr($arr)
  4. {
  5. print_r($arr);
  6. echo '<hr>';
  7. }
  8. //将一组数组分割成每个长度为2的数组
  9. printArr(array_chunk($arr, 2));
  10. //结果:Array ( [0] => Array ( [0] => 1 [1] => 2 ) [1] => Array ( [0] => 3 [1] => 4 ) [2] => Array ( [0] => 5 ) )
  11. //从键名3开始填充5个值为'demo'的元素
  12. printArr(array_fill(3, 5, 'demo'));
  13. //结果:Array ( [3] => demo [4] => demo [5] => demo [6] => demo [7] => demo )
  14. //倒序生成数组
  15. printArr(array_reverse($arr));
  16. //结果:Array ( [0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1 )
  17. $arr = ['id' => 1, 'username' => 'admin', 'email' => 'admin@php.cn', 'password' => 'admin'];
  18. //获取所有键名组成的数组
  19. printArr(array_keys($arr));
  20. //结果:Array ( [0] => id [1] => username [2] => email [3] => password )
  21. //判断数组中是否存在指定键名
  22. echo array_key_exists('address', $arr) ? '存在<hr>' : '不存在<hr>';
  23. //结果:不存在
  24. //获取数组第一个和最后一个键名
  25. echo array_key_first($arr) . '/';
  26. echo array_key_last($arr) . '<hr>';
  27. //结果:id/password
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