首頁技術(shù)文章正文

合并,拆分,追加,查找,刪除等

更新時間:2018-10-26 來源:黑馬程序員技術(shù)社區(qū) 瀏覽量:

  1. 合并數(shù)組

    array_merge()函數(shù)將數(shù)組合并到一起,返回一個聯(lián)合的數(shù)組。所得到的數(shù)組以第一個輸入數(shù)組參數(shù)開始,按后面數(shù)組參數(shù)出現(xiàn)的順序依次迫加。其形式為:

    array array_merge (array array1 array2…,arrayN)
    這個函數(shù)將一個或多個數(shù)組的單元合并起來,一個數(shù)組中的值附加在前一個數(shù)組的后面。返回作為結(jié)果的數(shù)組。

    如果輸入的數(shù)組中有相同的字符串鍵名,則該鍵名后面的值將覆蓋前一個值。然而,如果數(shù)組包含數(shù)字鍵名,后面的值將不會覆蓋原來的值,而是附加到后面。

    如果只給了一個數(shù)組并且該數(shù)組是數(shù)字索引的,則鍵名會以連續(xù)方式重新索引。

    <?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. 追加數(shù)組

    array_merge_recursive()函數(shù)與array_merge()相同,可以將兩個或多個數(shù)組合并在一起,形成一個聯(lián)合的數(shù)組.兩者之間的區(qū)別在于,當某個輸入數(shù)組中的某個鍵己經(jīng)存在于結(jié)果數(shù)組中時該函數(shù)會采取不同的處理方式.a(chǎn)rray_merge()會覆蓋前面存在的鍵/值對,替換為當前輸入數(shù)組中的鍵/值對,而array_merge_recursive()將把兩個值合并在一起,形成一個新的數(shù)組,并以原有的鍵作為數(shù)組名。還有一個數(shù)組合并的形式,就是遞歸追加數(shù)組。其形式為:

    array array_merge_recursive(array array1,array array2[…,array arrayN])
    程序?qū)嵗缦拢?br/>
    <?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 ) 
    ?>


    現(xiàn)在鍵 apple 指向一個數(shù)組,這個數(shù)組由兩個顏色值組成的索引數(shù)組。

    3. 連接數(shù)組

    array_combine()函數(shù)會得到一個新數(shù)組,它由一組提交的鍵和對應(yīng)的值組成。其形式為:

    array array_combine(array keys,array values)
    注意,兩個輸入數(shù)組必須大小相同,不能為空。示例如下

    <?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. 拆分數(shù)組 array_slice()

    array_slice()函數(shù)將返回數(shù)組中的一部分,從鍵offset開始,到offset+length位置結(jié)束。其形式:

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


    offset 為正值時,拆分將從距數(shù)組開頭的offset 位置開始;如果offset 為負值,則拆分從距數(shù)組末尾的offset 位置開始。如果省略了可選參數(shù)length,則拆分將從offset 開始,一直到數(shù)組的最后一個元素。如果給出了length 且為正數(shù),則會在距數(shù)組開頭的offset+length 位置結(jié)束。相反,如果給出了length且為負數(shù),則在距數(shù)組開頭的count(input_array)-|length|位置結(jié)束。考慮一個例子:

    <?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. 接合數(shù)組 array_splice()

    array_splice()函數(shù)會刪除數(shù)組中從offset開始到offset+length 結(jié)束的所有元素,并以數(shù)組的形式返回所刪除的元素。其形式為:

    array array_splice ( array array , int offset[,length[,array replacement]])
    offset 為正值時,則接合將從距數(shù)組開頭的offset 位置開始,offset 為負值時,接合將從距數(shù)組末尾的offset 位置開始。如果忽略可選的length 參數(shù),則從offset 位置開始到數(shù)組結(jié)束之間的所有元素都將被刪除。如果給出了length 且為正值,則接合將在距數(shù)組開頭的offset + leng th 位置結(jié)束。相反,如果給出了length且為負值,則結(jié)合將在距數(shù)組開頭的count(input_array)-length的位置結(jié)束。實例如下:

    <?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 ) 
    ?>
    可以使用可選參數(shù)replacement來指定取代目標部分的數(shù)組。實例如下:

    <?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 ) 
    ?>
    從程序可以很清楚看到這個函數(shù)的使用方法了。

    6. 數(shù)組的交集 array_intersect()

    array_intersect()函數(shù)返回一個保留了鍵的數(shù)組,這個數(shù)組只由第一個數(shù)組中出現(xiàn)的且在其他每個輸入數(shù)組中都出現(xiàn)的值組成。其形式如下:

    array array_intersect(array array1,array array2[,arrayN…])
    下面這個例子將返回在$fruit1數(shù)組中出現(xiàn)的且在$fruit2和$fruit3中也出現(xiàn)的所有的水果:

    <?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 ) 
    ?>
    只有在兩個元素相等且具有相同的數(shù)據(jù)類型時,array_intersect()函數(shù)才會認為它們是相同的。

    7. 關(guān)聯(lián)數(shù)組的交集 array_intersect_assoc()

    函數(shù)array_intersect_assoc()與array_intersect()基本相同,只不過他在比較中還考慮了數(shù)組的鍵。因此,只有在第一個數(shù)組中出現(xiàn),且在所有其他輸入數(shù)組中也出現(xiàn)的鍵/值對才返回到結(jié)果數(shù)組中。

    形式如下:

    array array_intersect_assoc(array array1,array array2[,arrayN…])
    下面的例子返回了出現(xiàn)在$fruit1數(shù)組中,也同時出現(xiàn)在$fruit2與$fruit3中的所有鍵/值對:

    <?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. 數(shù)組的差集 array_diff()

    函數(shù)array_diff()返回出現(xiàn)在第一個數(shù)組中但其他輸入數(shù)組中沒有的值。這個功能與array_intersect()相反。

    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. 關(guān)聯(lián)數(shù)組的差集 array_diff_assoc()

    函數(shù)array_diff_assoc()與array_diff()基本相同,只是它在比較時還考慮了數(shù)組的鍵。因此,只在第一個數(shù)組中出現(xiàn)而不再其他輸入數(shù)組中出現(xiàn)的鍵/值對才會返回到結(jié)果數(shù)組中。其形式如下:

    array array_diff_assoc(array array1,array array2[,arrayN…])
    下面的例子只返回了[yellow] => Banana,因為這個特殊的鍵/值對出現(xiàn)在$fruit1中,而在$fruit2和$fruit3中都不存在。

    <?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 ) 
    ?>
    使用數(shù)組的過程中經(jīng)常要遍歷數(shù)組。通常需要遍歷數(shù)組并獲得各個鍵或值(或者同時獲得鍵和值),所以毫不奇怪,PHP為此提供了一些函數(shù)來滿足需求。許多函數(shù)能完成兩項任務(wù),不僅能獲取當前指針位置的鍵或值,還能將指針移向下一個適當?shù)奈恢谩?br/>
    10. 獲取當前數(shù)組鍵 key()

    key()函數(shù)返回input_array中當前指針所在位置的鍵。其形式如下:

    mixed key(array array)
    下面的例子通過迭代處理數(shù)組并移動指針來輸出$fruits數(shù)組的鍵:

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


    // apple  
    // banana
    注意,每次調(diào)用key()時不會移動指針。為此需要使用next()函數(shù),這個函數(shù)的唯一作用就是完成推進指針的任務(wù)。

    11. 獲取當前數(shù)組值 current()

    current()函數(shù)返回數(shù)組中當前指針所在位置的數(shù)組值。其形式如下:

    mixed current(array array)
    下面修改前面的例子,這一次我們要獲取數(shù)組值:

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


    // red  
    // yellow
    12. 獲取當前數(shù)組鍵和值 each()

    each()函數(shù)返回input_array的當前鍵/值對,并將指針推進一個位置。其形式如下:

    array each(array array)
    返回的數(shù)組包含四個鍵,鍵0和key包含鍵名,而鍵1和value包含相應(yīng)的數(shù)據(jù)。如果執(zhí)行each()前指針位于數(shù)組末尾,則返回false。

    $fruits = array("apple", "banana", "orange", "pear"); 
    print_r ( each($fruits) ); 

    // Array ( [1] => apple [value] => apple [0] => 0 [key] => 0 )
    each() 經(jīng)常和 list() 結(jié)合使用來遍歷數(shù)組。本例與上例類似,不過循環(huán)輸出了整個數(shù)組:

    $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
    因為將一個數(shù)組賦值給另一個數(shù)組時會重置原來的數(shù)組指針,因此在上例中如果我們在循環(huán)內(nèi)部將 $fruits 賦給了另一個變量的話將會導(dǎo)致無限循環(huán)。

    這就完成了數(shù)組的遍歷。

    查找、篩選與搜索數(shù)組元素是數(shù)組操作的一些常見功能。下面來介紹一下幾個相關(guān)的函數(shù)。

    13. in_array()函數(shù)

    in_array()函數(shù)在一個數(shù)組匯總搜索一個特定值,如果找到這個值返回true,否則返回false。其形式如下

    boolean in_array(mixed needle,array haystack[,boolean strict]);
    來看下面的例子,查找變量apple是否已經(jīng)在數(shù)組中,如果在,則輸出一段信息:

    $fruit = "apple";  
    $fruits = array("apple","banana","orange","pear");  
    if( in_array($fruit,$fruits) )   
        echo "$fruit 已經(jīng)在數(shù)組中";
    第三個參數(shù)可選,它強制in_array()在搜索時考慮類型。

    14. array_key_exists()函數(shù)

    如果在一個數(shù)組中找到一個指定的鍵,函數(shù)array_key_exists()返回true,否則返回false。其形式如下:

    boolean array_key_exists(mixed key,array array);
    下面的例子將在數(shù)組鍵中搜索apple,如果找到,將輸出這個水果的顏色:

    $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()函數(shù)

    array_search()函數(shù)在一個數(shù)組中搜索一個指定的值,如果找到則返回相應(yīng)的鍵,否則返回false。其形式如下:

    mixed array_search(mixed needle,array haystack[,boolean strict])
    下面的例子在$fruits中搜索一個特定的日期(December 7),如果找到,則返回相應(yīng)州的有關(guān)信息:

    $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()函數(shù)

    array_keys()函數(shù)返回一個數(shù)組,其中包含所搜索數(shù)組中找到的所有鍵。其形式如下:

    array array_keys(array array[,mixed search_value])
    如果包含可選參數(shù)search_value,則只會返回與該值匹配的鍵。下面的例子將輸出$fruit數(shù)組中找到的所有數(shù)組:

    $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()函數(shù)

    array_values()函數(shù)返回一個數(shù)組中的所有值,并自動為返回的數(shù)組提供數(shù)值索引。其形式如下:

    array array_values(array array)
    下面的例子將獲取$fruits中找到的各元素的值:

    $fruits["apple"] = "red"; 
    $fruits["banana"] = "yellow"; 
    $fruits["watermelon"]="green"; 
    $values = array_values($fruits); 
    print_r($values); 

    //Array ( [0] => red [1] => yellow [2] => green )
    有時候我們需要擴展一個數(shù)組,或者刪掉數(shù)組的一部分,PHP為擴展和縮小數(shù)組提供了一些函數(shù)。對于那些希望模仿各種隊列實現(xiàn)(FIFO、LIFO)的程序員來說,這些函數(shù)可以提供便利。顧名思義,從這些函數(shù)的函數(shù)名(push、pop、shift和unshift)就清楚地反映出其作用。

    PS:傳統(tǒng)的隊列是一種數(shù)據(jù)結(jié)構(gòu),刪除元素與加入元素的順序相同,就稱為先進先出,或FIFO。相反,棧是另外一種數(shù)據(jù)結(jié)構(gòu),其中刪除元素的順序與加入時的順序相反,這成為后進先出,或LIFO。

    18. 在數(shù)組頭添加元素

    array_unshift()函數(shù)在數(shù)組頭添加元素。所有己有的數(shù)值鍵都會相應(yīng)地修改,以反映其在數(shù)組中的新位置,但是關(guān)聯(lián)鍵不受影響。其形式如下:

    int array_unshift(array array,mixed variable[,mixed variable])
    下面這個例子在$fruits數(shù)組前面添加了兩種水果:

    $fruits = array("apple","banana"); 
    array_unshift($fruits,"orange","pear") 
    // $fruits = array("orange","pear","apple","banana");
    19. 在數(shù)組尾添加元素

    array_push()函數(shù)的返回值是int型,是壓入數(shù)據(jù)后數(shù)組中元素的個數(shù),可以為此函數(shù)傳遞多個變量作為參數(shù),同時向數(shù)組壓入多個變量。其形式為:

    (array array,mixed variable [,mixed variable...])
    下面這個例子在$fruits數(shù)組中又添加了兩個水果:

    $fruits = array("apple","banana"); 
    array_push($fruits,"orange","pear") 
    //$fruits = array("apple","banana","orange","pear")
    20. 從數(shù)組頭刪除值

    array_shift()函數(shù)刪除并返回數(shù)組中找到的元素。其結(jié)果是,如果使用的是數(shù)值健,則所有相應(yīng)的值都會下移,而使用關(guān)聯(lián)鍵的數(shù)組不受影響。其形式為

    mixed array_shift(array array)
    下面的例子刪除了$fruits數(shù)組中的第一個元素apple:

    $fruits = array("apple","banana","orange","pear"); 
    $fruit = array_shift($fruits); 
    // $fruits = array("banana","orange","pear") 
    // $fruit = "apple";
    21. 從數(shù)組尾刪除元素

    array_pop()函數(shù)刪除并返回數(shù)組的最后一個元素。其形式為:

    mixed array_pop(aray target_array);
    下面的例子從$states數(shù)組刪除了最后的一個州:

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


    本文版權(quán)歸黑馬程序員PHP+H5全棧工程師培訓(xùn)學(xué)院所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請注明作者出處。謝謝!

    作者:PHP+H5全棧工程師培訓(xùn)學(xué)院
    首發(fā):http://java.itcast.cn/?skc

分享到:
在線咨詢 我要報名
和我們在線交談!