PHP - array_merge没有考虑键

时间:2014-03-12 16:01:45

标签: php arrays merge

我需要有关php数组合并的帮助

我需要按值合并,

所以如果我在数组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 )

所以它总是每个元素的最大数量,例如所有数组中的book

非常感谢,

马立克

2 个答案:

答案 0 :(得分: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',
)

大型阵列应该很慢。

答案 1 :(得分: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