Blogger Information
Blog 30
fans 0
comment 0
visits 13876
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5个数组函数实例演示
天宁
Original
537 people have browsed it
array_chunk(),将一个数组分割成多个
  1. $arr = array('a', 'b', 'c', 'd', 'e');
  2. //切割成每个小数组为两个的二维数组
  3. printf('<pre>%s</pre>', print_r(array_chunk($arr, 2), true));
  4. echo '<hr>';
  5. //切割的时候,原来的键值不变动
  6. printf('<pre>%s</pre>', print_r(array_chunk($arr, 2, true), true));
array_count_values(),统计数组内相同的值数量
  1. $array = array(1, "hello", 1, "world", "hello");
  2. printf('<pre>%s</pre>', print_r($array, true));
  3. printf('<pre>%s</pre>', print_r(array_count_values($array), true));
  4. //返回,键名变成了数组内的原来的值,值代表重复的次数
  5. Array
  6. (
  7. [1] => 2
  8. [hello] => 2
  9. [world] => 1
  10. )
array_merge()合并一个或多个数组
  1. $array1 = array("color" => "red", 2, 4);
  2. $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
  3. $result = array_merge($array1, $array2);
  4. printf('<pre>%s</pre>', print_r($result, true));
array_intersect(),用于比较两个(或更多)数组的值,返回这些数组内都存在值组成的数组
  1. $array1 = array("a" => "green", "red", "blue");
  2. $array2 = array("b" => "green", "yellow", "red");
  3. $result = array_intersect($array1, $array2);
  4. printf('<pre>%s</pre>', print_r($result, true));
array_reverse(),返回单元顺序相反的数组
  1. $input = array("php", 4.0, array("green", "red"));
  2. $reversed = array_reverse($input);
  3. $preserved = array_reverse($input, true);
  4. printf('<pre>%s</pre>', print_r($input, true));//打印原本的数组
  5. printf('<pre>%s</pre>', print_r($reversed, true));//把数组倒序,键名重置
  6. printf('<pre>%s</pre>', print_r($preserved, true));//把数组倒序,键名跟随之前的值,不改变
array_fill(),用给定的值填充数组
  1. //array_fill(数组起始索引,插入元素的数量,用来填充的值)
  2. $b = array_fill(5, 6, 'banana');
  3. $c = array_fill(-2, 4, 'pear');
  4. printf('<pre>%s</pre>', print_r($b, true));
  5. printf('<pre>%s</pre>', print_r($c, true));
array_fill_keys(),使用指定的键和值填充数组
  1. $keys = array('foo', 5, 10, 'bar');
  2. //array_fill_keys(使用该数组的值作为键,填充使用的值)
  3. $a = array_fill_keys($keys, 'banana');
  4. printf('<pre>%s</pre>', print_r($a, 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