0

I have an array something like below. I want to merge the nested array and display the totals. Here ID = 60.I want to merge this [0] and [1] depending in the ID value i.e., 60.

Array(
    [0] => Array
    (
        [ID] => 60
        [TOTAL] => 500
    )
    [1] => Array
    (
        [ID] => 60
        [TOTAL] => 600
    )
   [2] => Array
    (
        [ID] => 61
        [TOTAL] => 600
    )

   )

I tried with two for loops

foreach($result as $key=>$value){
    foreach($result as $key1 => $value1){
    // Do stuffs here
    }
}

I want the output as

Array(
     [0] => Array(
           [ID] =>60
           [TOTAL] => 1100
     )
     [1] =>Array(
           [ID] =>61
           [TOTAL] => 600
     )
  )
2
  • Quick question: Do you get this array from a database query? Commented Jul 24, 2019 at 15:54
  • @vivek_23 No, I get this by merging two arrays. Used array_merge() function and got this Commented Jul 24, 2019 at 15:56

3 Answers 3

2
<?php

$result = Array(
     Array
    (
        'ID' => 60,
        'TOTAL' => 500
    ),
    Array
    (
        'ID' => 60,
        'TOTAL' => 600
    ),
   Array
    (
        'ID' => 61,
        'TOTAL' => 600
    )
   );

$set = [];   


foreach($result as $data){
    if(!isset($set[$data['ID']])) $set[$data['ID']] = 0;
    $set[$data['ID']] += $data['TOTAL'];
}

$result_set = [];

foreach($set as $id => $total){
    $result_set[] = [
        'ID' => $id,
        'TOTAL' => $total
    ];
}

print_r($result_set);

Demo: https://3v4l.org/NMmHC

  • We store IDs as keys in our $set array and keep adding the total to it whenever we come across the same key in the foreach loop.
  • In the end, we collect the results in a new array with ID and it's respective total.
Sign up to request clarification or add additional context in comments.

Comments

1

My suggestion is to use array_reduce() which iteratively reduce the array to a single value using a callback function.

$arr = [['ID' => 60,'TOTAL' => 500],['ID' => 60,'TOTAL' => 600],['ID' => 61,'TOTAL' => 600]];

$arr = array_reduce($arr, function($acc, $new) {
    if (!isset($acc[$new['ID']])) {
        $acc[$new['ID']] = $new;
        return $acc;
    }

    $acc[$new['ID']]['TOTAL'] += $new['TOTAL'];
    return $acc;
}, []);

echo '<pre>', print_r(array_values($arr));

Working demo.

Comments

0

Solution 1

$arrayvariable=Array(
    [0] => Array
    (
        [ID] => 60
        [TOTAL] => 500
    )
    [1] => Array
    (
        [ID] => 60
        [TOTAL] => 600
    )
   [2] => Array
    (
        [ID] => 61
        [TOTAL] => 600
    )
   )
$output = array_reduce($arrayvariable, function (array $compare, array $item) {
        $intrestkey = $item ['ID'];
        if (array_key_exists($intrestkey, $compare)) {
            $compare [$intrestkey] ['TOTAL'] += $item ['TOTAL'];
        } else {
            $compare [$intrestkey] = $item;
        }
        return $compare;
    }, array());

 $incremetedmerged_array = array_values($output);

print_r($incremetedmerged_array); //Produces

Array(
     [0] => Array(
           [ID] =>60
           [TOTAL] => 1100
     )
     [1] =>Array(
           [ID] =>61
           [TOTAL] => 600
     )
  )

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.