0

I have an array like this:

Array (
    [0] => Array
        (
            [code] => Red Rose
            [info] => 2
        )

    [1] => Array
        (
            [code] => Lily Flower
            [info] => 1
        )

    [2] => Array
        (
            [code] => Lily Flower
            [info] => 4
        )

    [3] => Array
        (
            [code] => Lily Flower
            [info] => 2
        )
)

I need my result to be like:

Red Rose (2)

Lily Flower (7)

I have been trying to use foreach, but with no success.

foreach($array as $rowjb):
   echo $rowjb['code']." (".$rowjb['info'].") </br>";
endforeach;

Red Rose (2)
Lily Flower (1)
Lily Flower (4)
Lily Flower (2)
3
  • Use the code as a key and sum the info for each iteration. Post $array as executable code and Ill show example.... if this is coming from a DB this could be done much easier. Commented Jan 20, 2021 at 16:58
  • Because you're just looping through the array you need to sum the values with the same 'code' values that array items have. Commented Jan 20, 2021 at 16:59
  • What have you tried to check why the given code does not aggregate anything? Commented Jan 20, 2021 at 17:16

3 Answers 3

3

I would loop through your array and create a new array in the format you need.

$new_array = [];
foreach($array as $row) {
    
    $code = $row['code'];
    $info = $row['info'];

    //check if `code` key exists in `$new_array`, if not, create it
    if(!array_key_exists($code, $new_array)) {
        $new_array[$code] = 0;
    }
    
    //add up `info` values per-key
    $new_array[$code] += $info;
}

/* result looks like
    Array
    (
        [Red Nose] => 2
        [Lily Flower] => 7
    )
*/

This will create a new array with your code value as the key, and will add up all the numbers per-key for info.

Then, getting your output is trivial;

foreach($new_array as $code => $info) {
    echo "{$code} ({$info})<br>";
}

Output

Red Nose (2)<br>Lily Flower (7)<br>
Sign up to request clarification or add additional context in comments.

Comments

0

You will have to accumulate the total and work out when the code changes

$array = [['code'=>'Red Nose', 'info'=> 2],
        ['code'=>'Lily Flower', 'info'=> 1],
        ['code'=>'Lily Flower', 'info'=> 4],
        ['code'=>'Lily Flower', 'info'=> 2]
    ];

$last_code = $array[0]['code'];
$abacus = 0;
foreach( $array as $rowjb){
    
    if ( $last_code != $rowjb['code'] ) {
        echo $last_code . " (" . $abacus . ") <br>\n";    
        $abacus = 0;
        $last_code = $rowjb['code'];
    }

    $abacus += $rowjb['info'];
}
echo $last_code . " (" . $abacus . ") <br>\n"; 

RESULT

Red Nose (2) <br>
Lily Flower (7) <br>

Comments

0

You can use an intermediate array, to store associate each value for your codes (providing they are valid array keys). And then sum them.

<?php

$array = [
    ['code'=>'Red Rose', 'info'=> 2],
    ['code'=>'Lily Flower', 'info'=> 1],
    ['code'=>'Lily Flower', 'info'=> 4],
    ['code'=>'Lily Flower', 'info'=> 2]
];

foreach($array as $item) {
    $code_infos[$item['code']][] = $item['info'];
}

$code_info_sums = array_map('array_sum', $code_infos);
var_export($code_info_sums);

Output:

array (
    'Red Rose' => 2,
    'Lily Flower' => 7,
  )

The intermediate array:

var_export($code_infos);

Output:

array (
  'Red Rose' => 
  array (
    0 => 2,
  ),
  'Lily Flower' => 
  array (
    0 => 1,
    1 => 4,
    2 => 2,
  ),
)

To display you can just loop through the resultant array.

foreach($code_info_sums as $code => $count) {
    echo "$code ($count)\n";
}

Output:

Red Rose (2)
Lily Flower (7)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.