PHP 的 array merge 與 + 號的差別

來源:互聯網
上載者:User
PHP 的 array merge 與 + 號的區別

PHP 的 array_merge 會將 數值 變成 0, 1, 2..., 就算是強制轉換成字串也一樣.

範例 - 使用 array_merge

$a1 = array(
??? '9' => '0',
??? '311' => '1',
??? '快樂' => '2',
??? '2009a' => '3');

$a2 = array(
??? '2009' => '11',
??? '聖誕節' => '22',
??? '111a' => '33');

$amerge = array();

$amerge = array_merge($a1, $a2);

print_r($amerge);
?>

輸出結果

Array
(
??? [0] => 0
??? [1] => 1
??? [快樂] => 2
??? [2009a] => 3
??? [2] => 11
??? [聖誕節] => 22
??? [111a] => 33
)

於 官方網站查(array_merge), 使用 "+" 就可以 merge, 而且會將 key(hash、index) 值保留.

範例 - 使用 + 合并

$a1 = array(
??? '9'???? => '0',
??? '311'?? => '1',
??? '快樂'?? => '2',
??? '2009a' => '3'
);

$a2 = array(
??? '2009'? => '11',
??? '聖誕節' => '22',
??? '111a'? => '33'
);

$amerge = array();

$amerge = $a1 + $a2;

print_r($amerge);
?>

輸出結果

Array
(
??? [9] => 0
??? [311] => 1
??? [快樂] => 2
??? [2009a] => 3
??? [2009] => 11
??? [聖誕節] => 22
??? [111a] => 33
)
原文網站 / 轉載自: Tsung Hao

This work, unless otherwise expressly stated, is licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.