php數組相加 array(“a”)+array(“b”)結果還是array(“a”)_php技巧

來源:互聯網
上載者:User

在網上看到一道題:

array("a")+array("b")的結果是___

A.array("a","b")
B.array("b","a")
C.array("b")
D.array("a")

答案:D

php兩個數組相加,為什麼結果還是不變呢?

因為,它們等效於array("0″=>"a")+array("0″=>"b"),它們的鍵名相同,前者不能被後者覆蓋,如果是array("0″=>"a")+array("0″=>"b","1″=>"c"),那麼結果等於array("0″=>"a","1″=>"c")

如果是同一個數組裡面,有相同的鍵名會出現什麼情況?

看一段php官方手冊中的代碼:

複製代碼 代碼如下:
$switching = array(         10, // key = 0
                    5    =>  6,
                    3    =>  7, 
                    'a'  =>  4,
                            11, // key = 6 (maximum of integer-indices was 5)
                    '8'  =>  2, // key = 8 (integer!)
                    '02' => 77, // key = '02'
                    0    => 12  // the value 10 will be overwritten by 12
                  );

可見,同一個數組裡面如果有相同的鍵名,則前面一個鍵名的值將會被覆蓋(overwritten)。

聯繫我們

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