3

I want to merge two array's using key(product_id) and adding that values(usage). Array 1

Array
(
    [0] => Array
        (
            [name] => Reschedule A Service 
            [usage] => 1
            [product_id] => 8
        )

    [1] => Array
        (
            [name] => Adding An Image
            [usage] => 1
            [product_id] => 5
        )

    [2] => Array
        (
            [name] => Each Calendar Event
            [usage] => 1
            [product_id] => 14
        )

)

Array 2

Array
(
    [0] => Array
        (
            [name] => Adding An Image
            [usage] => 1
            [product_id] => 5
        )

    [1] => Array
        (
            [name] => Schedule A Service
            [usage] => 3
            [product_id] => 11
        )

    [2] => Array
        (
            [name] => Each Calendar Event
            [usage] => 2
            [product_id] => 14
        )

    [3] => Array
        (
            [name] => Sales Performance Dashboard
            [usage] => 2
            [product_id] => 30
        )

    [4] => Array
        (
            [name] => Quote
            [usage] => 1
            [product_id] => 32
        )

)

I need an out put like this merging and adding usage values.

Array
(
    [0] => Array
        (
            [name] => Adding An Image
            [usage] => 2
            [product_id] => 5
        )

    [1] => Array
        (
            [name] => Schedule A Service
            [usage] => 3
            [product_id] => 11
        )

    [2] => Array
        (
            [name] => Each Calendar Event
            [usage] => 3
            [product_id] => 14
        )

    [3] => Array
        (
            [name] => Sales Performance Dashboard
            [usage] => 2
            [product_id] => 30
        )

    [4] => Array
        (
            [name] => Quote
            [usage] => 1
            [product_id] => 32
        )

    [5] => Array
        (
            [name] => Reschedule A Service 
            [usage] => 1
            [product_id] => 8
        )

)

This is my code for creating arrays

    foreach($query->rows as $product){

     $top_products[]=array(
        'name'=>$product['name'],
        'usage'=>$product['pusage'],
        'product_id'=>$product['product_id']
     );

    }

    foreach($query_2->rows as $product){        

         $top_point_products[]=array(
           'name'=>$product['name'],
           'usage'=>$product['p_usage'],
           'product_id'=>$product['product_id']
         );

    }

4
  • Please post your code too Commented Jun 7, 2017 at 7:15
  • Possible duplicate of Merging two multidimensional arrays on specific key Commented Jun 7, 2017 at 7:25
  • @Daniyal thats a different one here i need to add usage values,There they are merging only title with name Commented Jun 7, 2017 at 7:37
  • @ASR I have added my code also Commented Jun 7, 2017 at 7:46

4 Answers 4

1
$first =array(

    array(
        "name" => "Reschedule A Service",
        "usage" => 1,
        "product_id" => 8
    ),
    array(
        "name" => "Adding An Image",
        "usage" => 1,
        "product_id" => 5
    ),
    array(
        "name" => "Each Calendar Event",
        "usage" => 1,
        "product_id" => 14
    )
    );
$second =array(


    array(
        "name" => "Adding An Image",
        "usage" => 1,
        "product_id" => 5
    ),
    array(
        "name" => "Schedule A Service",
        "usage" => 3,
        "product_id" => 11
    ),
    array(
        "name" => "Each Calendar Event",
        "usage" => 2,
        "product_id" => 14
    ),
    array(
        "name" => "Sales Performance Dashboard",
        "usage" => 2,
        "product_id" => 30
    ),
    array(
        "name" => "Quote",
        "usage" => 1,
        "product_id" => 32
    )
);

$result = array_unique(array_merge($first,$second), SORT_REGULAR);

Use array_unique & array_merge

Sign up to request clarification or add additional context in comments.

10 Comments

Yes but when i use array_unique i cannot add usage value of two arrays
I want add two arrays while merging for example product_id=>5 in 1 st array need to add usage values with product_id=>5 in second array
do you want to overwrite second array's usage value ?
if yes then just replace the first and second array like this
$result = array_unique(array_merge($second,$first), SORT_REGULAR);
|
0

Use the array_merge function, like this:

$C = array_merge($A, $B);
print_r($C);

Read manual Array merge

Comments

0

try this code

<?php

$array1=array
(
    0 => array(
            'name' => "Reschedule A Service", 
            'usage' => 1,
            'product_id' => 8
        ),

    1 => Array
        (
            'name' => "Adding An Image",
            'usage' => 1,
            'product_id' => 5
        ),

    2 => Array
        (
            'name' => "Each Calendar Event",
            'usage' => 2,
            'product_id' => 14
        )

);

$array2=array
(
    0 => Array
        (
            'name' => "Adding An Image",
            'usage' => 1,
            'product_id' => 5
        ),

    1 => Array
        (
            'name' => "Schedule A Service",
            'usage' => 3,
            'product_id' => 11
        ),

    2 => Array
        (
            'name' => "Each Calendar Event",
            'usage' => 5,
            'product_id' => 14
        ),

    3 => Array
        (
            'name' => "Sales Performance Dashboard",
            'usage' => 2,
            'product_id' => 30
        ),

    4 => Array
        (
            'name' => "Quote",
            'usage' => 1,
            'product_id' => 32
        )

);

$product_id1=array_column($array1, 'product_id');  
$product_id2=array_column($array2, 'product_id');  

$new=array_intersect($product_id1,$product_id2);

foreach ($new as $key => $value) {
    if(in_array($new[$key],$product_id2)){
        $array2[array_search($new[$key],$product_id2)]['usage']+=$array1[$key]['usage'];
      }
}
$new1=array_diff($product_id1,$product_id2);
foreach ($new1 as $key => $value) {
        $array2[]=$array1[$key];
 }

foreach ($array2 as $key => $value) {
    echo "[".$key."]=><br>";
    foreach ($value as $key1 => $value1) {
        echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
        echo "[".$key1."]=>".$value1."<br>";

    }
    echo "<br>";
}
?>

output

[0]=> [name]=>Adding An Image [usage]=>2 [product_id]=>5

[1]=> [name]=>Schedule A Service [usage]=>3 [product_id]=>11

[2]=> [name]=>Each Calendar Event [usage]=>7 [product_id]=>14

[3]=> [name]=>Sales Performance Dashboard [usage]=>2 [product_id]=>30

[4]=> [name]=>Quote [usage]=>1 [product_id]=>32

[5]=> [name]=>Reschedule A Service [usage]=>1 [product_id]=>8

Comments

0

Use array_merge and a simple foreach loop to check your condition and update the usagevalues.

See below

$result = array_merge($arrArray1, $arrArray2);

$result2 = array(); 

foreach($result as $key => $value){
        if(array_key_exists($value['product_id'], $result2)){
               $result2[$value['product_id']]['usage'] +=  $value['usage'];
        } else{
            $result2[$value['product_id']] = $value;
        }
}

print_r($result2);

If you want to reset your resultant array indexes use array_merge again like this

$result2 = array_merge($result2); 

Hope this will help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.