在两者中合并具有索引权重的两个数组

时间:2011-09-06 13:27:29

标签: php arrays

我有两个像这样的数组,想要合并像

获取第一个索引数组1检查

  • 如果它存在于数组2中
    插入数组3中 数组1和数组2的 添加
  • 否则,如果不存在
    获取数组2的第一个索引

获取数组2的第一个索引并检查

  • 如果它存在于阵列1中 插入数组3中 数组1和数组2的 添加
  • 否则,如果不存在
    获取数组1的第二个索引

获取数组1的第二个索引并检查

  • 如果它存在于数组2中
    插入数组3中 数组1和数组2的 添加
  • 否则,如果不存在
    获取数组2的第一个索引

获取数组2的第一个索引并检查

注意上面的逻辑是给定的例子 例如

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
    )
)

我希望结果为

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
    )
)

先谢谢

3 个答案:

答案 0 :(得分:2)

$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);

此外,如果您使用未知数量的阵列,则可以执行此操作:

$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);

答案 1 :(得分:1)

你似乎首先需要第二个数组,
然后附加第一个数组

也许这是有效的

$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);

<强>结果

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
        )

)

答案 2 :(得分:1)

这是一项非常简单的任务:

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

给出输入:

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

会产生:

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