开发者

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开发者_高级运维

  • if it is present in Array 2

    Insert in Array 3

    add weight of Array 1 and Array 2

  • else if not present

    take 1st index of Array 2

Take 1st index of Array 2 and check

  • if it is present in Array 1

    Insert in Array 3

    add weight of Array 1 and Array 2

  • else if not present

    take 2nd index of Array 1

Take 2nd index of Array 1 and check

  • if it is present in Array 2

    Insert in Array 3

    add weight of Array 1 and Array 2

  • else if not present

    take 1st index of Array 2

Take 1st index of Array 2 and check

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜