PHP合并數組追加數組例子

來源:互聯網
上載者:User

  PHP合并數組我們可以使用array_merge()函數,array_merge()函數返回一個聯合的數組。所得到的數組以第一個輸入數組參數開始,按後面數組參數出現的順序依次追加。其形式為:

  array array_merge (array array1 array2…,arrayN)

  下面是一個PHP合并數組的例子:

  1

  2$fruits = array("apple","banana","pear");

  3$numbered = array("1","2","3");

  4$cards = array_merge($fruits, $numbered);

  5print_r($cards);

  6// 輸出結果:

  7// Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 )

  8?>

  用PHP追加數組,使用array_merge_recursive(),將兩個數組合并在一起,注意,與array_merge()函數是不一樣的,array_merge()的兩個數組有重複項時會覆蓋掉,而array_merge_recursive()則不會。array_merge_recursive()文法:

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

  下面是一個PHP追加數組的例子:

  view sourceprint?1

  2$fruit1 = array("apple" => "red", "banana" => "yellow");

  3$fruit2 = array("pear" => "yellow", "apple" => "green");

  4$result = array_merge_recursive($fruit1, $fruit2);

  5print_r($result);

  6// 輸出結果:

  7// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow )

  8?>

  現在apple 指向一個數組,由兩個顏色值組成的索引數組。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.