Blogger Information
Blog 37
fans 1
comment 0
visits 32421
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php常用数组函数以及数组的交集,差集,并集
Jason Pu?
Original
1380 people have browsed it

一.常用php数组函数

1.array_search()函数检测数组中是否存在指定的值,若找到返回键名,如果没找到则返回false,第三个参数true为可选参数,即检测值又检测类型

  1. $x = array_search('6',[1,2,3,4,5,6]);
  2. var_dump($x);//int(5)


2.array_slice(数组,开始位置,[提取的个数],[保留或重置键名])指定位置提取元素,第三个参数如果是true就保留键名,false,重置键名

  1. $arr = array('张三'=>90,'李四'=>91,'王五'=>92,'赵六'=>93);
  2. $a = array_slice($arr,3);//提取从指定位置开始向后的所有元素
  3. $b = array_slice($arr,1,2);//从开始位置提取的个数
  4. print_r($a);//Array ( [赵六] => 93 )
  5. echo "<br>";
  6. print_r($b);//Array ( [李四] => 91 [王五] => 92 )

3.explode(匹配分割点,要分割的字符串,个数)函数把字符串分割为数组

  1. $str="www.php.cn/v/1.mp4||www.php.cn/v/2.mp4||www.php.cn/v/3.mp4";
  2. $arr1 = explode("||",$str);
  3. print_r($arr1);// Array ( [0] => www.php.cn/v/1.mp4 [1] => www.php.cn/v/2.mp4 [2] => www.php.cn/v/3.mp4 )

4.array_chunk(数组,新数组包含多少个元素,[true/false])把一个数组分割为新的数组,返回一个多维的索引数组,true/false:可选,默认是false重置键名,true:保留原始数组中的键名

  1. $arr = array('张三'=>90,'李四'=>91,'王五'=>92,'赵六'=>93);
  2. $new_arr = array_chunk($arr,3,true);
  3. print_r($new_arr);//Array ( [0] => Array ( [张三] => 90 [李四] => 91 [王五] => 92 ) [1] => Array ( [赵六] => 93 ) )

5.array_merge(数组1,数组2…)函数把多个数组合并为一个数组,如果两个或多个数组元素有相同的键名,最后的会覆盖前面的

  1. $arr2 = ['小明'=>99,'小张'=>95,'小刘'=>96];
  2. $arr3 = ['小李'=>99,'小张'=>98,'小刘'=>97];
  3. $new_arr1 = array_merge($arr2,$arr3);
  4. print_r($new_arr1);//Array ( [小明] => 99 [小张] => 98 [小刘] => 97 [小李] => 99 )

6.array_merge_recursive(数组1,数组2…)也是把一个或多少个数组合并为一个数组,但和array_merge()不同的是,如果有相同的键名,会将多个相同键名的值递归组成一个数组

  1. $new_arr2 = array_merge_recursive($arr2,$arr3);
  2. print_r($new_arr2);//Array ( [小明] => 99 [小张] => Array ( [0] => 95 [1] => 98 ) [小刘] => Array ( [0] => 96 [1] => 97 ) [小李] => 99 )

7.array_combine(键名数组,键值数组)合并两个数组成为一个新数组,一个数组是键名,一个数组是键名,两个数组元素个数必须相同

  1. $arr4 = ['小张','小陈','小李','小红','小梅'];
  2. $arr5 = [100,99,86,87,98];
  3. $new_arr3 = array_combine($arr4,$arr5);
  4. print_r($new_arr3);//Array ( [小张] => 100 [小陈] => 99 [小李] => 86 [小红] => 87 [小梅] => 98 )

8.array_rand(数组,数量)返回数组中的随机键名,如果规定函数返回的不止一个键名,则返回包含随机键名的数组
8.1:随机取一个:

  1. $rand1 = array_rand($new_arr3);
  2. print_r($rand1);//小红

8.2:随机取多个:

  1. $rand2 = array_rand($new_arr3,2);
  2. print_r($rand2);//Array ( [0] => 小红 [1] => 小梅 )

9.sort(数组)对索引数组进行升序排序,会重置索引,会改变原数组,成功返回true

  1. $arr_num=[95,82,88,96,70,100];
  2. sort($arr_num);
  3. print_r($arr_num);//Array ( [0] => 70 [1] => 82 [2] => 88 [3] => 95 [4] => 96 [5] => 100 )

10.rsort()对索引数组进行降序排列,其它特性和sort一样,类似的还有ksort()键值按升序排列,krsort()键值按降序排列


11.shuffle()按键值对数组进行随机排序,会直接改变原数组,会重置键名,返回一个布尔值
11.1:索引数组随机排序:

  1. $arr_num1 = [1,2,3,4,5,6];
  2. shuffle($arr_num1);
  3. print_r($arr_num1);//Array ( [0] => 5 [1] => 6 [2] => 2 [3] => 4 [4] => 1 [5] => 3 )

11.2:关联数组随机排序,不改变原关联关系:

  1. $arr_num2 = ['a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6];
  2. function array_rs($arr){
  3. $a = $arr; //复制原数组
  4. shuffle($arr); //按键值对数组进行随机排序
  5. $k = [];
  6. foreach ($arr as $value){
  7. $b = array_keys($a,$value);//获取原数组中对应的键名
  8. $k = array_merge($k,$b);//合并数组
  9. }
  10. return array_combine($k,$arr);//合并键名和键值
  11. };
  12. $array1 = array_rs($arr_num2);
  13. print_r($array1);//Array ( [a] => 1 [b] => 2 [e] => 5 [d] => 4 [f] => 6 [c] => 3 )

12.array_count_values()用于统计数组中所有值出现的次数,返回一个关联数组,键名:原数组的值,键值:出现次数

  1. $arr_num3=[1,1,1,3,5,5,6,6,66,66,7,77,77];
  2. $count_num=array_count_values($arr_num3);
  3. print_r($count_num);//Array ( [1] => 3 [3] => 1 [5] => 2 [6] => 2 [66] => 2 [7] => 1 [77] => 2 )

二.数组的交集,差集,并集

1.交集:

  1. $arr6 = [1,2,3,4,5,6];
  2. $arr7 = [4,5,6,7,8,9];

array_intersect(arr1,arr2…)比较两个或多个数组的键值,并返回交集
array_intersect_key()比较两个或多个数组的键名交返交集
array_intersect_assoc()函数用于比较两个(或更多个)数组的键名和键值,并返回交集
例如计算上面数组的交集:

  1. $result1 = array_intersect($arr6,$arr7);
  2. print_r($result1);//Array ( [3] => 4 [4] => 5 [5] => 6 )

2.差集:

array_diff()用于比较两个或多个数组的键值,并返回差集,返回在被比较数组中,但在其它数组中没有的键值
array_key()比较两个或多个数组的键名并返回差集
array_diff_assoc()比较两个或多个数组的键名和键值并返回差集
例如:

  1. $result2 = array_diff($arr6,$arr7);
  2. print_r($result2);//Array ( [0] => 1 [1] => 2 [2] => 3 )

3.并集:

比较键名返回并集,可以用array_merge()

  1. $arr8 = ['a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6];
  2. $arr9 = ['a'=>4,'b'=>5,'c'=>6,'k'=>7,'l'=>8,'m'=>9];
  3. $result3 = array_merge($arr8,$arr9);
  4. print_r($result3);//Array ( [a] => 4 [b] => 5 [c] => 6 [d] => 4 [e] => 5 [f] => 6 [k] => 7 [l] => 8 [m] => 9 )
Correcting teacher:天蓬老师天蓬老师

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