Blogger Information
Blog 87
fans 1
comment 0
visits 58531
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
常用数组函数学习
阿杰
Original
841 people have browsed it

1、数组遍历函数

(1)快速遍历foreach

  1. $user3 = ['id'=>1,'name'=>'Jack','age'=>28,'address'=>'深圳龙华'];
  2. foreach($user3 as $key=>$value){
  3. printf('[%s]=>%s<hr>',$key,$value);
  4. }

(2) 结构遍历foreach

  1. // 解构遍历通常用来遍历二维或者以上的数组
  2. $user4 = [
  3. ['id'=>1,'name'=>'小明'],
  4. ['id'=>2,'name'=>'张三'],
  5. ['id'=>3,'name'=>'李四'],
  6. ];
  7. foreach($user4 as list('id'=>$id,'name'=>$name)){
  8. printf('$id=%s,$name=%s<hr>',$id,$name);
  9. }

2、数组统计函数

(1)相加 array_sum

  1. // 相加
  2. function sum(...$args)
  3. {
  4. return array_sum($args);
  5. }
  6. echo sum(1,2,3,4,5,6,7).'<hr>';

(2)相乘 array_product

  1. // 相乘
  2. function mul(...$args)
  3. {
  4. return array_product($args);
  5. }
  6. echo '相乘积为:'.mul(2,3,4,5).'<hr>';

3、数组查询、删除、更新、添加

(1)数组查询 array_slice

  1. $user = ['id'=>1,'name'=>'张三','age'=>18,'course'=>'php','grade'=>90];
  2. printf('<pre>%s<hr></pre>',print_r($user,true));
  3. // array_slice
  4. // 前两个
  5. $res = array_slice($user,0,2);
  6. printf('<pre>%s<hr></pre>',print_r($res,true));
  7. // 后两个
  8. $res2 = array_slice($user,-2,2);
  9. // $res2 = array_slice($user,-2,1);
  10. printf('<pre>%s<hr></pre>',print_r($res2,true));

(2)数组删除 array_splice

  1. // array_splice
  2. $arr = [1,2,3,4,5,6,7,8,9,10];
  3. printf('<pre>%s<hr></pre>',print_r($arr,true));
  4. // 删除:第2个位置删除2个
  5. $res3 = array_splice($arr,1,2);
  6. printf('<pre>%s<hr></pre>',print_r($res3,true));
  7. printf('<pre>%s<hr></pre>',print_r($arr,true));

(3)数组更新

  1. // 更新:第2个位置删除2个,使用新的数据来替换掉它
  2. $res4 = array_splice($arr,1,2,['A','B']);
  3. printf('<pre>%s<hr></pre>',print_r($res4,true));
  4. printf('<pre>%s<hr></pre>',print_r($arr,true));

(4)数组添加

  1. // 添加: 第2个位置删除0个,传入的新数据会追加到当前位置的后面
  2. $res5 = array_splice($arr,1,0,['hello','world']);
  3. printf('<pre>%s<hr></pre>',print_r($res5,true));
  4. printf('<pre>%s<hr></pre>',print_r($arr,true));

4、数组回调函数

(1)过滤器 array_filter

  1. // array_filter: 仅返回数组中可转为true的元素集合
  2. $arr = [
  3. 150,
  4. 'php',
  5. true,
  6. [4, 5, 6],
  7. (new class
  8. {
  9. }),
  10. [],
  11. null,
  12. false,
  13. '',
  14. 0,
  15. '0'
  16. ];
  17. $res = array_filter($arr,function($item){
  18. if($item){
  19. // 检测变量是否是一个标量描述
  20. return is_scalar($item);
  21. }
  22. });
  23. printf('<pre>%s<hr></pre>',print_r($res,true));

(2)过滤器 array_map

  1. // array_map
  2. $arr2 = ['php', [3, 4, 5], (new class
  3. {
  4. public $name = '电脑';
  5. public $price = 8888;
  6. }), 15, 20];
  7. printf('<pre>%s<hr></pre>',print_r($arr2,true));
  8. $res2 = array_map(function($item){
  9. switch(gettype($item)){
  10. case 'array':
  11. $item = join(',',$item);
  12. break;
  13. case 'object':
  14. $item = join(',',get_object_vars($item));
  15. }
  16. return $item;
  17. },$arr2);
  18. printf('<pre>%s<hr></pre>',print_r($res2,true));

(3)归并 array_reduce

  1. // 3.归并
  2. $arr3 = [10,20,30,40,50];
  3. $res3 = array_reduce($arr3,function($acc,$cur){
  4. echo $acc, ', ', $cur, '<br>';
  5. return $acc + $cur;
  6. },0);
  7. echo $res3.'<hr>';

(4)array_walk

  1. // 4.array_walk
  2. $user = ['id' => 10, 'name' => 'admin', 'email' => 'admin@php.cn'];
  3. array_walk($user,function($value,$key,$color){
  4. printf('[%s]=><span style="color:%s">%s</span>', $key, $color, $value);
  5. },'orange');

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