## **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
)
)
```