懷疑:php array_merge_recursive的一個bug

來源:互聯網
上載者:User
$adiff = json_decode('{"28":0,"29":0,"30":0,"31":0,"32":0,"33":0,"34":0,"35":0}',true);$diff = json_decode('{"28":1,"29":1,"30":1,"31":1,"32":1,"33":1,"34":1,"35":1}',true);$adiff = array_merge_recursive($diff,$adiff);var_dump($adiff);

理論上應該返回:

array(16) { [28]=> int(1) [29]=> int(1) [20]=> int(1) [31]=> int(1) [32]=> int(1) [33]=> int(1) [34]=> int(1) [35]=> int(1) [36]=> int(0) [37]=> int(0) [38]=> int(0) [39]=> int(0) [40]=> int(0) [41]=> int(0) [42]=> int(0) [43]=> int(0) }

實際返回:

array(16) { [0]=> int(1) [1]=> int(1) [2]=> int(1) [3]=> int(1) [4]=> int(1) [5]=> int(1) [6]=> int(1) [7]=> int(1) [8]=> int(0) [9]=> int(0) [10]=> int(0) [11]=> int(0) [12]=> int(0) [13]=> int(0) [14]=> int(0) [15]=> int(0) }

這算不算一個bug?

更新:
這個不算bug,只是手冊裡面沒有對數字鍵進行merge時會重設說明清楚。

回複內容:

$adiff = json_decode('{"28":0,"29":0,"30":0,"31":0,"32":0,"33":0,"34":0,"35":0}',true);$diff = json_decode('{"28":1,"29":1,"30":1,"31":1,"32":1,"33":1,"34":1,"35":1}',true);$adiff = array_merge_recursive($diff,$adiff);var_dump($adiff);

理論上應該返回:

array(16) { [28]=> int(1) [29]=> int(1) [20]=> int(1) [31]=> int(1) [32]=> int(1) [33]=> int(1) [34]=> int(1) [35]=> int(1) [36]=> int(0) [37]=> int(0) [38]=> int(0) [39]=> int(0) [40]=> int(0) [41]=> int(0) [42]=> int(0) [43]=> int(0) }

實際返回:

array(16) { [0]=> int(1) [1]=> int(1) [2]=> int(1) [3]=> int(1) [4]=> int(1) [5]=> int(1) [6]=> int(1) [7]=> int(1) [8]=> int(0) [9]=> int(0) [10]=> int(0) [11]=> int(0) [12]=> int(0) [13]=> int(0) [14]=> int(0) [15]=> int(0) }

這算不算一個bug?

更新:
這個不算bug,只是手冊裡面沒有對數字鍵進行merge時會重設說明清楚。

手冊中提到了:

If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

如果兩個數組存在相同的數字鍵名,則會在數組最後建立一個索引(雖然手冊中沒提到索引會重設)。

這種情況下推薦使用array_replace_recursive(PHP≥5.3)。

  • 聯繫我們

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