## **1. 合并数组**
**语法:**
1. array\_merge (array array1 array2…,arrayN)
2. array_merge_recursive(array array1,array array2[…,array arrayN])
#### **array_merge_recursive()** 函数与 **array_merge()** 相同,可以将两个或多个数组合并在一起,形成一个联合的数组.两者之间的区别在于,当某个输入数组中的某个键己经存在于结果数组中时该函数会采取不同的处理方式.**array_merge()** 会覆盖前面存在的键/值对,替换为当前输入数组中的键/值对,而**array_merge_recursive()** 将把两个值合并在一起,形成一个新的数组,并以原有的键作为数组名。
```
$fruits = array("apple","banana","pear");
$numbered = array("1","2","3");
$cards = array_merge($fruits, $numbered);
print_r($cards);
// output
// Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 )
```
```
$fruit1 = array("apple" => "red", "banana" => "yellow");
$fruit2 = array("pear" => "yellow", "apple" => "green");
$result = array_merge_recursive($fruit1, $fruit2);
print_r($result);
// output
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow )
```
*****
## **2. 合并数组(第一个做键名,第二个数组做值)**
**语法:**
array_combine(array keys,array values)
```
$name = array("apple", "banana", "orange");
$color = array("red", "yellow", "orange");
$fruit = array_combine($name, $color);
print_r($fruit);
// output
// Array ( [apple] => red [banana] => yellow [orange] => orange )
```
*****
## **3. 拆分数组**
**语法:**
array_slice (array array, int offset[,int length])
```
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon");
$subset = array_slice($fruits, 3);
print_r($subset);
// output
// Array ( [0] => Pear [1] => Grape [2] => Lemon [3] => Watermelon )
```
*****
## **4. 删除数组**
**语法:**
array_splice ( array array , int offset[,length[,array replacement]])
```
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon");
$subset = array_splice($fruits, 2, -1, array("Green Apple", "Red Apple"));
print_r($fruits);
print_r($subset);
// output
// Array ( [0] => Apple [1] => Banana [2] => Green Apple [3] => Red Apple [4] => Watermelon )
// Array ( [0] => Orange [1] => Pear [2] => Grape [3] => Lemon )
```
*****
## **5. 在第一个数组中存在的值**
**语法:**
1. array_intersect(array array1,array array2[,arrayN…])
2. array_intersect_assoc(array array1,array array2[,arrayN…])
```
$fruit1 = array("Apple","Banana","Orange");
$fruit2 = array("Pear","Apple","Grape");
$fruit3 = array("Watermelon","Orange","Apple");
$intersection = array_intersect($fruit1, $fruit2, $fruit3);
print_r($intersection);
// output
// Array ( [0] => Apple )
```
```
$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");
$fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");
$fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");
$intersection = array_intersect_assoc($fruit1, $fruit2, $fruit3);
print_r($intersection);
// output
// Array ( [red] => Apple )
```
#### 函数**array_intersect_assoc()** 与 **array_intersect()** 基本相同,只不过他在比较中还考虑了数组的键。因此,只有在第一个数组中出现,且在所有其他输入数组中也出现的键/值对才返回到结果数组中。
*****
## **6. 数组的差集**
**语法:**
1. array_diff(array array1,array array2[,arrayN…])
2. array_diff_assoc(array array1,array array2[,arrayN…])
```
$fruit1 = array("Apple","Banana","Orange");
$fruit2 = array("Pear","Apple","Grape");
$fruit3 = array("Watermelon","Orange","Apple");
$intersection = array_diff($fruit1, $fruit2, $fruit3);
print_r($intersection);
// output
// Array ( [1] => Banana )
```
```
$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");
$fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");
$fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");
$intersection = array_diff_assoc($fruit1, $fruit2, $fruit3);
print_r($intersection);
// output
// Array ( [yellow] => Banana )
```
*****
## **7. 数组中查找特定值**
**语法:**
1. in_array(mixed needle,array haystack[,boolean strict]);
2. array_search(value,array,strict)
#### 函数 **in_array** 查找数组中有值则返回true,否则返回false;函数**array_search** 查找值数组中有值返回数组的键名,没有则返回false
```
$fruit = "apple";
$fruits = array("apple","banana","orange","pear");
if( in_array($fruit,$fruits) )
echo "$fruit 已经在数组中";
```
```
$fruits = ['pear','ddd','123',123,232,1];
$c = array_search(123,$fruits);
// $c = 2;
```
*****
## **8. 数组中查找特定值**
**语法:**
1. boolean array_key_exists(mixed key,array array);
2. boolean isset(array[mixed])
#### 函数**array_key_exists()** 与函数 **isset()** 函数,都可以判断一个变量是否存在或被设置
```
$fruit["apple"] = "red";
$fruit["banana"] = "yellow";
$fruit["pear"] = "green";
if(array_key_exists("apple", $fruit)){
printf("apple's color is %s",$fruit["apple"]);
}
//apple's color is red
```
```
$fruits = ['pear','ddd','123',123,232,1];
$c = isset($fruits[2]);
var_dump($c);
// bool(true)
```
*****
## **9. 拿到数组中的键名**
**语法:**
array array_keys(array array[,mixed search_value])
```
$fruits["apple"] = "red";
$fruits["banana"] = "yellow";
$fruits["watermelon"]="green";
$keys = array_keys($fruits);
print_r($keys);
//Array ( [0] => apple [1] => banana [2] => watermelon )
```
*****
## **10. 拿到数组中的值(重置键名)**
**语法:**
array array_values(array array)
```
$fruits["apple"] = "red";
$fruits["banana"] = "yellow";
$fruits["watermelon"]="green";
$values = array_values($fruits);
print_r($values);
//Array ( [0] => red [1] => yellow [2] => green )
```
*****
## **11. 在数组头添加元素**
**语法:**
**int** array_unshift(array array,mixed variable[,mixed variable])
```
$fruits = array("apple","banana");
array_unshift($fruits,"orange","pear")
// $fruits = array("orange","pear","apple","banana");
```
*****
## **12. 在数末尾添加元素**
**语法:**
**int** array_push(array array,mixed variable [,mixed variable...])
```
$fruits = array("apple","banana");
array_push($fruits,"orange","pear")
//$fruits = array("apple","banana","orange","pear")
```
*****
## **13.从数组头删除值**
**语法:**
**mixed** array_shift(array array)
返回被删除的值
```
$fruits = array("apple","banana","orange","pear");
$fruit = array_shift($fruits);
// $fruits = array("banana","orange","pear")
// $fruit = "apple";
```
*****
## **14.删除数组尾元素**
**语法:**
**mixed** array_pop(aray target_array) 返回被删除的值
```
$fruits = array("apple","banana","orange","pear");
$fruit = array_pop($fruits);
//$fruits = array("apple","banana","orange");
//$fruit = "pear";
```
*****
## **15.数组分割**
**语法:**
**mixed** array_chunk(array,size,preserve_keys);返回被删除的值
```
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2));
Array
(
[0] => Array
(
[0] => Volvo
[1] => BMW
)
[1] => Array
(
[0] => Toyota
[1] => Honda
)
[2] => Array
(
[0] => Mercedes
[1] => Opel
)
)
```