简体   繁体   中英

How to merge two array in according to value in PHP?

I have two array and I need to merge it together !!

Array 1

Array
(
  [0] => Array
      (
        [brand] => CARTIER
        [amount_2014] => 136476
      )

  [1] => Array
    (
        [brand] => TIFFANY & CO.
        [amount_2014] => 22000
    )
  [2] => Array
    (
        [brand] => Test
        [amount_2014] => 33000
    )


 )

Array 2

   Array
 (
   [0] => Array
    (
        [brand] => CARTIER
        [amount_2013] => 22052
    )
   [1] => Array
    (
        [brand] => Test
        [amount_2013] => 3313
    )

 )

I need the result array as:

 Array
   (
    [0] => Array
      (
        [brand] => CARTIER
        [amount_2014] => 136476
        [amount_2013] => 22052
      )

    [1] => Array
      (
        [brand] => TIFFANY & CO.
        [amount_2014] => 22000
        [amount_2013] => 0
    )

   [2] => Array
      (
        [brand] => Test
        [amount_2014] => 33000
        [amount_2013] => 3313
    )

 )

So for every [brand] I need the amount in [amount_2014] & [amount_2013], if any one is not present then I need it value as 0;

I am using CodeIgniter for this project, I have only one table with field (brand, amount, year) am issuing two queries for getting sum of amount for 2013 & 2014.

(I am fighting with array_merge and array_combine but no use for me in this case, if any one can help with query then it's also very much helpful).

Try this:

function cars_array_merge()
{
    $arrays = func_get_args();
    foreach ($arrays as &$array)
    {
        $new_arr = array();
        foreach ($array as $value)
        {
            $brand = $value['brand'];
            unset($value['brand']);
            $new_arr[$brand] = $value;
        }
        $array = $new_arr;
    }
    $arrays = call_user_func_array('array_merge_recursive', $arrays);
    foreach ($arrays as $brand => &$array)
        $array['brand'] = $brand;
    return array_values($arrays);
}

// testing

$arr1 = [
    [ 'brand' => 'CARTIER', 'mount_2014' => 136476 ],
    [ 'brand' => 'TIFFANY & CO.', 'mount_2014' => 22000 ]
];

$arr2 = [
    [ 'brand' => 'CARTIER', 'mount_2013' => 22052 ]
];

print_r(cars_array_merge($arr1, $arr2));

Output:

Array
(
    [0] => Array
        (
            [mount_2014] => 136476
            [mount_2013] => 22052
            [brand] => CARTIER
        )

    [1] => Array
        (
            [mount_2014] => 22000
            [brand] => TIFFANY & CO.
        )
)
<?php

for ($i = 0, $max = count($arr1); $i < $max; ++$i) {
    $arr1[$i]['amount_2013'] = isset($arr2[$i]['amount_2013']) ? $arr2[$i]['amount_2013'] : 0;
}
$merge = array_merge($arr1, $arr2);
$temp = $merge;
$newArr = array(); $key_array = array();
foreach($merge as $key=>$val){
    $b = $val['brand'];
    $a1 = isset($val['amount_2013']) ? $val['amount_2013'] : 0;
    $a2 = isset($val['amount_2014']) ? $val['amount_2014'] : 0;
    unset($temp[$key]);
    foreach($temp as $k=>$values){
        if($values['brand'] == $b){
            if($a1 == 0){
                $a1 = isset($values['amount_2013']) ? $values['amount_2013'] : 0;
            }
            if($a2 == 0){
                $a2 = isset($values['amount_2014']) ? $values['amount_2014'] : 0;
            }
            unset($temp[$k]);
        }
    }
    if(!in_array($b, $key_array))
    {
        $newArr[] = array('brand' => $b, 'amount_2014' => $a2, 'amount_2013' => $a1);
    }
    $key_array[] = $b;
}
print_r($newArr);

This is what I used:

<?php
$arr1 = array(0=>array("brand"=>"CARTIER","amount_2014"=>136476), 1=>array("brand"=>"tiffany","amount_2014"=>22000)); 
$arr2 = array(0=>array("brand"=>"CARTIER","amount_2013"=>22000));

foreach ($arr2 as $key=>$value){

if( $value["brand"] == "CARTIER")
{
$arr1[0]["amount_2013"] = $value["amount_2013"];
$arr1[1]["amount_2013"] = 0;
}
else
{
$arr1[1]["amount_2013"] = $value["amount_2013"];
$arr1[0]["amount_2013"] = 0;
}

}

print_r($arr1);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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