PHP - array_merge没有考虑键

I need help with php array merging

I need merge by values,

so if I have values in array 1 like this

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

But if I have

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

So lets say, merged array values will be maximum count of each element, room for example , in all arrays

Few more examples, ( book ) ( book ) ( book ) = ( book ) ( room book book ) ( library room ) = ( library room book book ) ( room room ) ( book book book ) = ( room room book book book ) (room) ( room ) ( room room room ) = ( room room room )

I tried operator + , but then

( room room ) ( book book book ) is ( room room book book ), that is not good

I tried array_merge , but then

( room ) ( room ) is ( room ) , this is okay but ( room room ) ( room ) is ( room room room ) and that is wrong

It doesnt depend on order, and keys are not imporant, it is always numeric ( [0] [1] etc. )

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 )

So it is alway maximum count of each ellement, for example book in all arrays

Thank you very much,

Marek

dsz7121
dsz7121 ,它被编辑:)谢谢
6 年多之前 回复
dongwei2610
dongwei2610 你的数组符号不是很易读。你可以包含数组的print_r()输出,这样我们就可以更容易地看到数组的内容了吗?
6 年多之前 回复

2个回答

Here it is:

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;
    }

call like:

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

result:

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

Should be quite slow on large arrays.

douou8954
douou8954 +1,这是我第一次看到使用array_pad技巧的增量生成。 没有经过测试,但似乎它是构建结果数组最高效的方法。 太好了!
6 年多之前 回复

This would be a reasonable way to do it in 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.

dpdyh86002
dpdyh86002 看起来你的代码中有一个错误。 请发布您的确切代码,以便它运行并打印警告。
6 年多之前 回复
douzi3756
douzi3756 在这里,非常感谢ideone.com/mhcFXw
6 年多之前 回复
dsjbest2014
dsjbest2014 你怎么称呼它? 在ideone上发布一个示例。
6 年多之前 回复
dongyi2889
dongyi2889 我试着用它,但我收到了这个警告。 警告:array_merge_recursive()期望至少有一个参数,0在$ counts = array_map('array_count_values',$ deti)中给出; $巩固= call_user_func_array( 'array_merge_recursive',$计数); 和$ count打印正确
6 年多之前 回复
dpftppc9674
dpftppc9674 非常感谢 :)
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐