简体   繁体   English

合并两个索引权重的两个数组

[英]Merging two arrays with weight of index in both

I have two array like this and want to merge like 我有两个这样的数组,想要合并

Take 1st index Array 1 check 进行第一索引数组1检查

  • if it is present in Array 2 如果它在阵列2中存在
    Insert in Array 3 插入阵列3
    add weight of Array 1 and Array 2 增加数组1和数组2的权重
  • else if not present 否则,如果不存在
    take 1st index of Array 2 取数组2的第一个索引

Take 1st index of Array 2 and check 取数组2的第一个索引并检查

  • if it is present in Array 1 如果它存在于数组1中
    Insert in Array 3 插入阵列3
    add weight of Array 1 and Array 2 增加数组1和数组2的权重
  • else if not present 否则,如果不存在
    take 2nd index of Array 1 取数组1的第二个索引

Take 2nd index of Array 1 and check 取数组1的第二个索引并检查

  • if it is present in Array 2 如果它在阵列2中存在
    Insert in Array 3 插入阵列3
    add weight of Array 1 and Array 2 增加数组1和数组2的权重
  • else if not present 否则,如果不存在
    take 1st index of Array 2 取数组2的第一个索引

Take 1st index of Array 2 and check 取数组2的第一个索引并检查

Note above logic is for given example 请注意,以上逻辑仅供参考
For Example 例如

Array 1
(
    [144] => Array
    (
        [weight] => 2
    )
    [145] => Array
    (
        [weight] => 1
    )
    [177] => Array
    (
        [weight] => 1
    )
)

Array 2
(
    [93] => Array
    (
        [weight] => 4
    )
    [133] => Array
    (
        [weight] => 4
    )
    [144] => Array
    (
        [weight] => 4
    )
    [145] => Array
    (
        [weight] => 4
    )
    [141] => Array
    (
        [weight] => 1
    )
)

I want result as 我想要结果

Array 3
(
    [144] => Array
    (
        [weight] => 6
    )
    [145] => Array
    (
        [weight] => 5
    )
    [93] => Array
    (
        [weight] => 4
    )
    [133] => Array
    (
        [weight] => 4
    )
    [177] => Array
    (
        [weight] => 1
    )
    [141] => Array
    (
        [weight] => 1
    )
)

Thanks in Advance 提前致谢

$out = array();

foreach ($arr1 as $key => $val) {
  if (isset($out[$key]['weight'])) {
    $out[$key]['weight'] += $val['weight'];
  } else {
    $out[$key]['weight'] = $val['weight'];
  }
}

foreach ($arr2 as $key => $val) {
  if (isset($out[$key]['weight'])) {
    $out[$key]['weight'] += $val['weight'];
  } else {
    $out[$key]['weight'] = $val['weight'];
  }
}

print_r($out);

Also, if you have an unknown number of arrays to work with you can do this: 另外,如果您要使用的数组数量未知,则可以执行以下操作:

$arrays = array (
  $arr1,
  $arr2,
  $arr3
  // ...
};

$out = array();

foreach ($arrays as $array) {
  foreach ($array as $key => $val) {
    if (isset($out[$key]['weight'])) {
      $out[$key]['weight'] += $val['weight'];
    } else {
      $out[$key]['weight'] = $val['weight'];
    }
  }
}

print_r($out);

You seems to want second array first, 您似乎首先要第二个数组,
then append first array 然后追加第一个数组

maybe this is works 也许这是可行的

$a = ...; //first array
$b = ...; // second array

var_export($a);
var_export($b);

$rtn = $tmp = array();
foreach ($b as $idx=>$arr)
{
  if (isset($a[$idx]))
  {
    $rtn[$idx]['weight'] = $arr['weight'] + $a[$idx]['weight'];
    unset($a[$idx]);
  }
  else
  {
    $tmp[$idx]['weight'] = $arr['weight'];
  }
}

$res = $rtn+$tmp+$a;
print_r($res);

result 结果

array (
  144 =>
  array (
    'weight' => 2,
  ),
  145 =>
  array (
    'weight' => 1,
  ),
  177 =>
  array (
    'weight' => 1,
  ),
)array (
  93 =>
  array (
    'weight' => 4,
  ),
  133 =>
  array (
    'weight' => 4,
  ),
  144 =>
  array (
    'weight' => 4,
  ),
  145 =>
  array (
    'weight' => 4,
  ),
  141 =>
  array (
    'weight' => 1,
  ),
)Array
(
    [144] => Array
        (
            [weight] => 6
        )

    [145] => Array
        (
            [weight] => 5
        )

    [93] => Array
        (
            [weight] => 4
        )

    [133] => Array
        (
            [weight] => 4
        )

    [141] => Array
        (
            [weight] => 1
        )

    [177] => Array
        (
            [weight] => 1
        )

)

it's very simple task: 这是非常简单的任务:

array_walk($a, function(&$value, $key, $b){
    $value["weight"] += isset($b[$key]["weight"])?$b[$key]["weight"]:0;
}, $b);
$ret = $a+$b

given input: 给定输入:

$a = array(
    1 => array(
        "weight" => 1
    ),
    2 => array(
        "weight" => 5
    )
);
$b = array(
    1 => array(
        "weight" => 2
    ),
    3 => array(
        "weight" => 4
    )
);

would produce: 会产生:

array(3) {
  [1]=>
  array(1) {
    ["weight"]=>
    int(3)
  }
  [2]=>
  array(1) {
    ["weight"]=>
    int(5)
  }
  [3]=>
  array(1) {
    ["weight"]=>
    int(4)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM