0

Using array:

Array
(
    [0] => Array
        (
            [description] => EXAMI
            [pcchrgamt] => 190.00
            [pchrgqty] => 1.00
            [pchrgup] => 190.00
        )

    [1] => Array
        (
            [description] => EXAMI
            [pcchrgamt] => 40.00
            [pchrgqty] => 1.00
            [pchrgup] => 40.00
        )

)

I get :

Array
(
    [description] => 0
    [pcchrgamt] => 230
    [pchrgqty] => 2
    [pchrgup] => 230
)

Using :

print_r( $rows1 );
 $sumArray = array();
 foreach ($rows1 as $k=>$subArray) {

 foreach ($subArray as $id=>$value) {
 $sumArray[$id]+=$value;
}
}

print_r($sumArray);

What I wanted to get is :

Array
(
    [0] => Array
        (
            [description] => EXAMI
            [pcchrgamt] => 230
            [pchrgqty] => 2
            [pchrgup] => 230
        )



)

Meaning I just want to combine only numeric amount.

Also I dont know how to handle :

Array
(
    [0] => Array
        (
            [description] => EXAMI
            [pcchrgamt] => 190.00
            [pchrgqty] => 1.00
            [pchrgup] => 190.00
        )

    [1] => Array
        (
            [description] => EXAMI
            [pcchrgamt] => 40.00
            [pchrgqty] => 1.00
            [pchrgup] => 40.00
        )


    [2] => Array
        (
            [description] => EXAMI1
            [pcchrgamt] => 190.00
            [pchrgqty] => 1.00
            [pchrgup] => 190.00
        )

    [3] => Array
        (
            [description] => EXAMI1
            [pcchrgamt] => 40.00
            [pchrgqty] => 1.00
            [pchrgup] => 40.00
        )
)

I want to combine based on description and from above I want to get :

Array
    (
        [0] => Array
            (
                [description] => EXAMI
                [pcchrgamt] => 230
                [pchrgqty] => 2
                [pchrgup] => 230
            )
         [1] => Array
            (
                [description] => EXAMI1
                [pcchrgamt] => 230
                [pchrgqty] => 2
                [pchrgup] => 230
            )



    )

Description could be many , there could be many and there could be a unique at the same time.

How to proceed with these

4 Answers 4

3

Here's a fairly naive approach:

<?php
$records = [
    [
        'desc' => 'EXAMI',
        'amt' => 190.00,
        'qty' => 1,
        'up' => 190.00,
    ],
    [
        'desc' => 'EXAMI',
        'amt' => 40.00,
        'qty' => 1,
        'up' => 40.00,
    ],
    [
        'desc' => 'EXAMI1',
        'amt' => 190.00,
        'qty' => 1,
        'up' => 190.00,
    ],
    [
        'desc' => 'EXAMI1',
        'amt' => 40.00,
        'qty' => 1,
        'up' => 40.00,
    ],
];

function combineArrayOnKey(array $array, $key) {
    $new_array = [];

    foreach($array as $k => $v) {
        if(!is_array($v) || !isset($v[$key]))
            continue;

        // Create elements for new keys
        if(!isset($new_array[$v[$key]])) {
            $new_array[$v[$key]] = $v;
            continue;
        }

        $parent =& $new_array[$v[$key]];

        foreach($v as $kk => $vv) {
            if(!is_numeric($vv))
                continue;

            $parent[$kk] = floatval(@$parent[$kk]) + $vv;
        }
    }

    return array_values($new_array);
}

print_r(combineArrayOnKey($records, 'desc'));

This function loops through the records and creates a single entry for each distinct desc key. When it encounters a record with a duplicate desc, it loops through its numeric fields and sums them with the previous entries.

It gives this output from your example:

Array
(
    [0] => Array
        (
            [desc] => EXAMI
            [amt] => 230
            [qty] => 2
            [up] => 230
        )

    [1] => Array
        (
            [desc] => EXAMI1
            [amt] => 230
            [qty] => 2
            [up] => 230
        )

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

9 Comments

i want to index it with numeric not the description also in my result the index is blank let me check on a bit :)
That's easily handled. I wrapped the function's return value in array_values() to reindex the result with incremental keys. Regarding the example, you should be able to paste it into a new .php file and run it. Did you need to preserve the original record's numeric key?
im not sure why but i get this Array ( [0] => Array ( [description] => EXAMI [pcchrgamt] => 190.00 [pchrgqty] => 1.00 [pchrgup] => 190.00 ) [1] => Array ( [description] => EXAMI [pcchrgamt] => 40.00 [pchrgqty] => 1.00 [pchrgup] => 40.00 ) ) Array ( ) using print_r( $rows1 ); print_r(combineArrayOnKey($rows1, 'desc'));
You too! I'm glad to hear it helped. :)
Sure. Even cleaner would be: if(in_array($kk, ['pcchrgamt','pchrgqty,'pchrgup'])) { ... }. You could skip the else entirely if you don't care what values the other keys end up with. You'll keep the value from whatever the first occurrence of a given description was.
|
0

You can use the desc as key of the result, you also use array_values on the result to just number as key, you can check the live demo here

$result = [];
foreach($records as $v) {
  $result[$v['desc']]['desc']  = $v['desc'];
  $result[$v['desc']]['amt']  += $v['amt']; 
  $result[$v['desc']]['qty']  += $v['qty']; 
  $result[$v['desc']]['up']   += $v['up']; 
}
print_r($result);

4 Comments

That works fine if the data is always perfect, but: (1) it won't adapt automatically to new keys or different arrays since it's hardcoded, (2) it doesn't handle the case where an expected key isn't set yet, so you'd get a bunch of undefined index notices for each new entry.
you can add @ to escape the warning.
If one of the record values isn't numeric you'll also break your subtotals. As I said, your approach works if the conditions are perfect (which in my experience is usually a bad assumption).
Yeah, you're right. My answer is condition specific, and your smart answer is much more waterproof.
0

Try this :

$sumArray = array();

 foreach ($rows1 as $k=>$subArray) {

     foreach ($subArray as $key=> $value) {
    if(is_numeric($value)){
                $sumArray[$subArray['description']][$key]+=$value;
         }
         else{
               $sumArray[$subArray['description']][$key]=$value;
         }
     }

}

var_dump($sumArray);

Comments

-1

can you try this code instead of your code

    print_r( $rows1 );
     $sumArray = array();
     foreach ($rows1 as $k=>$subArray) {
        $sumArray[$subArray['description']['description'] = $subArray['description']
        $sumArray[$subArray['description']['amt'] += $subArray['amt']
        $sumArray[$subArray['description']['qty'] += $subArray['qty']
        $sumArray[$subArray['description']['up'] += $subArray['up']

    }

    print_r([$sumArray1, $sumArray2]);

and lemme know if any issues

3 Comments

I want to combine based on description and from above I want to get : this is one of my problem
the value is random i cant use static value FYI not my downvote
ok..even if you downvoted its fair if the solution does not work , but lemme think and get back to you

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.