2014-03-12 41 views
0

我需要用PHP數組合並PHP - array_merge不考慮鍵

我需要值合併幫助,

,所以如果我在陣列1的值這樣

(room room) and in array values (room) , merge will look like this = (room room) 

但是,如果我有

(room room book) and (book book) merged array values will look like this (room room book book) 

因此可以說,合併的數組值將是每個元素的最大數量,房間例如,在所有陣列

幾個例子, (書)(書)(書)=(書) (室書書)(的書房)=(庫室書書) (室室) (書書書)=(房間房間書書書) (室)(室)(房間房間房間)=(房間房間房間)

我試圖操作者+,但隨後

(室房間)(書籍書)是(房間房間預訂書),那不好

我試過array_merge,但隨後

(室)(室)是(室),這是好的 但 (客房)(室)是(室室室),這是錯誤的

它不依賴於順序,和鍵不項重要的,它始終是數字([0] [1]等)

ARRAYS:

   [0] => Array 
        (
         [0] => room 
         [1] => room 
        ) 

       [1] => Array 
        (
         [0] => book 
         [1] => book 
         [2] => book 
         [3] => book 
        ) 

EXPECTED RESULT = array (room, room, book , book , book, book) 

       [0] => Array 
        (
         [0] => room 
         [1] => room 
        ) 

       [1] => Array 
        (
         [0] => room 
        ) 

EXPECTED RESULT = array (room, room) 

       [0] => Array 
        (
         [0] => room 
        ) 

       [1] => Array 
        (
         [0] => room 


        ) 

EXPECTED RESULT = array (room) 

       [0] => Array 
        (
         [0] => room 
         [1] => room 
         [2] => book 
        ) 

       [1] => Array 
        (
         [0] => room 
         [1] => book 
        ) 
       [2] => Array 
        (
         [0] => room 
         [1] => room 
         [2] => room 
        ) 

EXPECTED RESULT = array (room , room ,room , book) 

所以常是在所有陣列

非常感謝你們每個ellement的最大計數,例如書籍,

馬立克

+2

您的數組表示法不易讀。你可以包含數組的'print_r()'輸出,所以我們更容易看到數組的內容? –

+0

@AmalMurali,它被編輯:)謝謝 – user3411282

回答

1

這就是:

function user_array_merge($arr) { 
    if (func_num_args() == 1) return $arr; 
    $vals = array_count_values($arr); 
    $arrs = func_get_args(); 
    array_shift($arrs); 
     foreach ($arrs as $arr2) { 
     $vals2 = array_count_values($arr2); 
     foreach ($vals2 as $val=>$count) { 
      $vals[$val] = @max($vals[$val], $count); 
     } 
     } 
     $result = array(); 
     foreach ($vals as $val=>$count) { 
     $result = array_pad($result, count($result)+$count, $val); 
     } 
     return $result; 
    } 

請致電:

$res = user_array_merge(['room','room'], ['book', 'book','book','room'],['library']); 

結果:

var_export($res);                      
array (
    0 => 'room', 
    1 => 'room', 
    2 => 'book', 
    3 => 'book', 
    4 => 'book', 
    5 => 'library', 
) 

應該對大型陣列很慢。

+0

+1,這是我第一次看到'array_pad'技巧的增量生成。未經測試,但它似乎是構建結果數組的最高性能方式。太好了! – Jon

0

這將是一個合理的方式做到這一點在O(N):

function custom_array_merge() { 
    // variable number of inputs 
    $arrays = func_get_args(); 

    // consolidate into array of items => count/array(of counts) across inputs 
    $counts = array_map('array_count_values', $arrays); 
    $consolidated = call_user_func_array('array_merge_recursive', $counts); 

    // reduce array of counts (where applicable) to maximum count 
    $maximums = array_map(function($v) { 
     return is_array($v) ? max($v) : $v; 
    }, $consolidated); 

    // build result -- could be faster with array_fill() and concatenating 
    // but that would definitely use up more memory 
    $result = []; 
    foreach ($maximums as $item => $times) { 
     for ($i = 0; $i < $times; ++$i) { 
      $result[] = $item; 
     } 
    } 

    return $result; 
} 

See it in action

+0

非常感謝:) – user3411282

+0

我試着用它,但我得到了這個警告。 警告:array_merge_recursive()需要至少1個參數,在 中給出的0 $ counts = array_map('array_count_values',$ deti); $ consolidated = call_user_func_array('array_merge_recursive',$ counts); 和$計數打印正確 – user3411282

+0

@ user3411282:你怎麼稱呼它?發表一個關於ideone的例子。 – Jon