douhuan6305 2014-02-13 23:25
浏览 12
已采纳

合并3个键=>值数组

i have these 3 arrays

Global array

array(5) { [0]=> array(1) { ["shout_id"]=> string(1) "4" } 
           [1]=> array(1) { ["shout_id"]=> string(1) "6" } 
           [2]=> array(1) { ["shout_id"]=> string(2) "16" } 
           [3]=> array(1) { ["shout_id"]=> string(2) "17" } 
           [4]=> array(1) { ["shout_id"]=> string(2) "20" } } 

Local array

array(1) { [0]=> array(1) { ["shout_id"]=> string(2) "13" } } 

Country array

array(1) { [0]=> array(1) { ["shout_id"]=> string(2) "19" } } 

and when i merge all 3 i get this

Result array

array(5) { [0]=> array(1) { ["shout_id"]=> string(2) "19" } 
           [1]=> array(1) { ["shout_id"]=> string(1) "6" } 
           [2]=> array(1) { ["shout_id"]=> string(2) "16" } 
           [3]=> array(1) { ["shout_id"]=> string(2) "17" } 
           [4]=> array(1) { ["shout_id"]=> string(2) "20" } }

However this is what i want

array(7) { [0]=> array(1) { ["shout_id"]=> string(2) "19" } 
           [1]=> array(1) { ["shout_id"]=> string(1) "6" } 
           [2]=> array(1) { ["shout_id"]=> string(2) "16" } 
           [3]=> array(1) { ["shout_id"]=> string(2) "17" } 
           [4]=> array(1) { ["shout_id"]=> string(2) "20" }
           [5]=> array(1) { ["shout_id"]=> string(2) "4" }
           [6]=> array(1) { ["shout_id"]=> string(2) "13" } }

For some reason it is missing out the values 4 and 13 and i can't work out why.

Here is the code for combining the arrays

$result_array = $country_array + $global_array + $local_array;
  • 写回答

2条回答 默认 最新

  • dongwu5318 2014-02-13 23:27
    关注

    Use array_merge, it concatenates arrays with numeric keys.

    $result_array = array_merge($country_array, $global_array, $local_array);
    

    + replaces elements with the same key.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?