数组判断操作
$phone = array('苹果','小米','华为','锤子','联想');
echo in_array('华为',$phone); // 1;存在返回1,不存在返回空
- array_key_exists 函数
判断 键值或索引 是否在数组中
$computer = array(
'联想'=>'Y900P',
'神州'=>'Z8',
'苹果'=>'Sierra',
);
echo array_key_exists('联想',$computer); // 1;存在返回1,不存在返回空
- array_search 函数
通过数组的元素值,返回对应的索引值
$phone = array('苹果','小米','华为','锤子','联想');
echo array_search('华为',$phone); //2
$computer = array(
'联想'=>'Y900P',
'神州'=>'Z8',
'苹果'=>'Sierra',
);
echo array_search('Y900P',$computer); //联想
数组转换操作
- array_keys 函数
提取数组中的 索引 作为元素值,组成一个新数组
<?php
$computer = array(
'联想'=>'Y900P',
'神州'=>'Z8',
'苹果'=>'Sierra',
);
$keys = array_keys($computer);
var_dump($keys);
![array_keys array_keys](https://img.php.cn/upload/image/838/653/139/1639901779497975.png)
- array_values 函数
提取数组中的 元素值 ,组成一个新数组
$computer = array(
'联想'=>'Y900P',
'神州'=>'Z8',
'苹果'=>'Sierra',
);
$keys = array_values($computer);
var_dump($keys);
![array_values array_values](https://img.php.cn/upload/image/650/171/398/1639901251122118.png)
数组拆合操作
- array_chunk 函数
将数组拆分成若干个多维数组
$phone = array('苹果','小米','华为','锤子','联想','魅族');
$chunk = array_chunk($phone,3); //每3个分为一组
print_r($chunk);
![array_chunk array_chunk](https://img.php.cn/upload/image/101/536/149/1639902580386951.png)
- array_merge 函数
将多个数组组合成一个数组
$phone = array('苹果','小米','华为','锤子','联想');
$computer = array(
'联想'=>'Y900P',
'神州'=>'Z8',
'苹果'=>'Sierra',
);
$merge = array_merge($phone,$computer);
print_r($merge);
![array_merge array_merge](https://img.php.cn/upload/image/337/741/188/1639903114783827.png)
- array_combine 函数(合并)
将两个相同元素个数的数组合并成一个数组,取数组 1 的值作为键,取数组 2 的值作为元素值
$color = array('green', 'red', 'yellow');
$fruit = array('pear', 'apple', 'banana');
$combine = array_combine($color, $fruit);
print_r($combine);
![array_combine array_combine](https://img.php.cn/upload/image/968/222/372/1639904098899260.png)
- array_intersect 函数
返回数组中的交集
$color1 =array('a'=>'green','red','blue');
$color2 = array('b'=>'green','yellow','red');
$array = array_intersect($color1,$color2); // 索引按数组第一个;索引根据这里数组的先后顺序
print_r($array);
![array_intersect array_intersect](https://img.php.cn/upload/image/545/411/285/1639904637947794.png)
$color1 =array('a'=>'green','red','blue');
$color2 = array('b'=>'green','yellow','red');
$array = array_diff($color1,$color2); // 根据第一个数组来对比第二个数组没有的返回
print_r($array);
![array_diff array_diff](https://img.php.cn/upload/image/982/264/471/1639905501764597.png)