Blogger Information
Blog 15
fans 0
comment 0
visits 6297
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组函数中5个小案例
啊℃。㏄
Original
451 people have browsed it

数组函数

array_chunk:一个数组分成多个

  1. // array_chunk():将一个数组分成多个
  2. $arr = [10,20,30,40,50];
  3. // array_chunk(数组,长度)
  4. $res = array_chunk($arr,2);
  5. printf('<pre>%s</pre>',print_r($res,true));

效果:

array_combine:创建新数组

  1. // array_combine():创建新数组,第一个值为键名,第二个值为 值
  2. $a = ['red','green','blue'];
  3. $b = ['abc','def','ghi'];
  4. $c = array_combine($a,$b);
  5. printf('<pre>%s</pre>',print_r($c,true));

效果:

array_count_values:统计数组的所有值

  1. // array_count_values():统计数组中所有的值
  2. // 报错:如果数组的类型不是字符串或者整数会有个警告
  3. $arr = ['hello',1,1,1,'hello','22'];
  4. $res = array_count_values($arr);
  5. printf('<pre>%s</pre>',print_r($res,true));

效果:

array_flip:交换数组中的键和值

  1. // array_flip():交换数组中的键和值
  2. $arr = [2,3,4];
  3. $res = array_flip($arr);
  4. printf('<pre>%s</pre>',print_r($res,true));
  5. echo '<hr>';
  6. $arr = ['a'=>1,'b'=>2,'c'=>3];
  7. $res = array_flip($arr);
  8. printf('<pre>%s</pre>',print_r($res,true));

效果:

array_intersect_assoc:带索引检查计算数组的交集

  1. // array_intersect_assoc():带索引检查计算数组的交集
  2. $a = ['a'=>'blue','b'=>'green','red','yellow'];
  3. $b = ['a'=>'blue','c'=>'green','red','pink'];
  4. $res = array_intersect_assoc($a,$b);
  5. printf('<pre>%s</pre>',print_r($res,true));

效果:

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