php 操作数组 (合并,拆分,追加,查找,删除等)

Posted 伊人世界

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 操作数组 (合并,拆分,追加,查找,删除等)相关的知识,希望对你有一定的参考价值。

出自 http://justcoding.iteye.com/blog/2281599

1. 合并数组

array array_merge (array array1 array2…,arrayN)
<?php  
$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 )  
?>  

 


 

 

2. 追加数组

array array_merge_recursive(array array1,array array2[…,array arrayN])  

<?php  
$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 )  
?>  

 

3. 连接数组

array array_combine(array keys,array values)  

<?php  
$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 )  
?>  

 

4. 拆分数组 array_slice()

array array_slice (array array, int offset[,int length])  

<?php  
  
$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 )  
?>  

<?php  
  
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon");  
$subset = array_slice($fruits, 2, -2);  
print_r($subset);  
  
// output  
// Array ( [0] => Orange [1] => Pear [2] => Grape )  
?>

 

 

5. 接合数组 array_splice()

array array_splice ( array array , int offset[,length[,array replacement]])  

<?php  
  
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon");  
$subset = array_splice($fruits, 4);  
  
print_r($fruits);  
print_r($subset);  
  
// output  
// Array ( [0] => Apple [1] => Banana [2] => Orange [3] => Pear )   
// Array ( [0] => Grape [1] => Lemon [2] => Watermelon )  
?>  

<?php  
  
$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 )  
?>  

 

6. 数组的交集 array_intersect()

array array_intersect(array array1,array array2[,arrayN…])  

<?php  
$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 )  
?>  

 

7. 关联数组的交集 array_intersect_assoc()

array array_intersect_assoc(array array1,array array2[,arrayN…])  

<?php  
$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 )  
?>  

 

8. 数组的差集 array_diff()

array array_diff(array array1,array array2[,arrayN…])  

<?php  
$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 )  
?>  

 

9. 关联数组的差集 array_diff_assoc()

array array_diff_assoc(array array1,array array2[,arrayN…])  

<?php  
$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 )  
?>  

 

10. 获取当前数组键 key()

mixed key(array array)  

$fruits = array("apple"=>"red", "banana"=>"yellow");  
while ($key = key($fruits)) {  
    printf("%s <br />", $key);  
    next($fruits);  
}  
  
// apple   
// banana  

 

11. 获取当前数组值 current()

mixed current(array array)  

$fruits = array("apple"=>"red", "banana"=>"yellow");  
while ($fruit = current($fruits)) {  
    printf("%s <br />", $fruit);  
    next($fruits);  
}  
  
// red   
// yellow   

 

12. 获取当前数组键和值 each()

array each(array array)  
$fruits = array("apple", "banana", "orange", "pear"); print_r ( each($fruits) ); // Array ( [1] => apple [value] => apple [0] => 0 [key] => 0 )
$fruits = array("apple", "banana", "orange", "pear");  
reset($fruits);  
  
while (list($key, $val) = each($fruits))  
    {  
        echo "$key => $val<br />";  
    }  
  
// 0 => apple  
// 1 => banana  
// 2 => orange  
// 3 => pear  

 


13. in_array()函数

boolean in_array(mixed needle,array haystack[,boolean strict]); 
$fruit = "apple"; $fruits = array("apple","banana","orange","pear"); if( in_array($fruit,$fruits) ) echo "$fruit 已经在数组中";

 

14. array_key_exists()函数

boolean array_key_exists(mixed key,array array);  

$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  

15. array_search()函数

mixed array_search(mixed needle,array haystack[,boolean strict])  

$fruits["apple"] = "red";  
$fruits["banana"] = "yellow";  
$fruits["watermelon"]="green";  
$founded = array_search("green", $fruits);  
if($founded)   
    printf("%s was founded on %s.",$founded, $fruits[$founded]);  
  
//watermelon was founded on green.  

 

 

16. array_keys()函数

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 )  

 

 

17. array_values()函数

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 )  

18. 在数组头添加元素

int array_unshift(array array,mixed variable[,mixed variable])  

$fruits = array("apple","banana");  
array_unshift($fruits,"orange","pear")  
// $fruits = array("orange","pear","apple","banana");  

19. 在数组尾添加元素

(array array,mixed variable [,mixed variable...])  

$fruits = array("apple","banana");  
array_push($fruits,"orange","pear")  
//$fruits = array("apple","banana","orange","pear")  

20. 从数组头删除值

mixed array_shift(array array)  

$fruits = array("apple","banana","orange","pear");  
$fruit = array_shift($fruits);  
// $fruits = array("banana","orange","pear")  
// $fruit = "apple";  

21. 从数组尾删除元素

mixed array_pop(aray target_array); 

$fruits = array("apple","banana","orange","pear");  
$fruit = array_pop($fruits);  
//$fruits = array("apple","banana","orange");  
//$fruit = "pear";   

 

以上是关于php 操作数组 (合并,拆分,追加,查找,删除等)的主要内容,如果未能解决你的问题,请参考以下文章

Thinkphp数组array常用函数

php 大数组合并去重 两个数组元素都过万

php 数组相同的元素组合并在一起

php如何查找数组中是不是含有某个元素?

CSV文件操作,PHP如何对满足条件的二维数组进行求和

js数组常用操作方法小结(增加,删除,合并,分割等)