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";   

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM