chakotha chakotha - 2 months ago 5
PHP Question

Collapse a multi-dimensional array by one key value while summing another key value

I have been making heavy weather of this but I am almost there.

I have a array of a list of employees (empcodes) with payment values and there are multiple entries of the same empcode with differing payment.

I am trying to collapse the duplicate empcodes while summing the payment value.

Here is example code with debugging echos:

<?


$temprows = array(
array('empcode' => 3222, 'userfirstname' => 'Ted', 'usersurname' => 'Smyth', 'value' => 1000.00),
array('empcode' => 3222, 'userfirstname' => 'Ted', 'usersurname' => 'Smyth', 'value' => 1337.93),
array('empcode' => 3422, 'userfirstname' => 'Lynn', 'usersurname' => 'Byrne', 'value' => 346.47),
array('empcode' => 3422, 'userfirstname' => 'Lynn', 'usersurname' => 'Byrne', 'value' => 6.95),
array('empcode' => 3436, 'userfirstname' => 'Dave', 'usersurname' => 'Smith', 'value' => 233),
array('empcode' => 3436, 'userfirstname' => 'Dave', 'usersurname' => 'Smith', 'value' => 3455),
);

$now=date("Y-m-d H:i:s");

$output_arr=[];
$empcodes_used=array();

$j=0;

foreach ($temprows as $arr) {

// if empcode not encountered yet
if (!in_array($arr["empcode"], $empcodes_used)) {

// Create new row
echo("new row empcode =".$arr["empcode"]."<br><br>");
$output_arr[$j]["empcode"]=$arr["empcode"];
$output_arr[$j]["userfirstname"]=$arr["userfirstname"];
$output_arr[$j]["usersurname"]=$arr["usersurname"];
$output_arr[$j]["value"]=$arr["value"];
$output_arr[$j]["paid"]=$now;

// Add emcpode to empcodes_used
array_push($empcodes_used, $arr["empcode"]);

$j++;

} else {
// IN ENGLISH: Find the $output_arr with this empcode and increment it's value by the value of this arr
echo("empcode used=".$arr["empcode"]."<br>");
$k=0;
foreach ($output_arr as $foo) {
echo("k=".$k."<br>");
echo("foo empcode=".$foo["empcode"]." and arr empcode=".$arr["empcode"]."<br>");
if ($foo["empcode"]==$arr["empcode"]) {
// Increment the payment value of that empcode
echo("foo empcode = arr empcode<br>");
$foo["value"]+=$arr["value"];
$output_arr[$k]["value"]=$foo["value"];
echo("foo value=".$foo["value"]."<br><br>");
$k++;
}

}
}

}
echo("<pre>");
print_r($output_arr);
echo("</pre>");
?>


This is the array I want out:

Array
(
[0] => Array
(
[empcode] => 3222
[userfirstname] => Ted
[usersurname] => Smyth
[value] => 2337.93
[paid] => 2016-09-15 10:51:30
)

[1] => Array
(
[empcode] => 3422
[userfirstname] => Lynn
[usersurname] => Byrne
[value] => 353.42
[paid] => 2016-09-15 10:51:30
)

[2] => Array
(
[empcode] => 3436
[userfirstname] => Dave
[usersurname] => Smith
[value] => 3688
[paid] => 2016-09-15 10:51:30
)

)


But this is what I am getting from the above code:

new row empcode =3222

empcode used=3222
k=0
foo empcode=3222 and arr empcode=3222
foo empcode = arr empcode
foo value=2337.93

new row empcode =3422

empcode used=3422
k=0
foo empcode=3222 and arr empcode=3422
k=0
foo empcode=3422 and arr empcode=3422
foo empcode = arr empcode
foo value=353.42

new row empcode =3436

empcode used=3436
k=0
foo empcode=3222 and arr empcode=3436
k=0
foo empcode=3422 and arr empcode=3436
k=0
foo empcode=3436 and arr empcode=3436
foo empcode = arr empcode
foo value=3688

Array
(
[0] => Array
(
[empcode] => 3222
[userfirstname] => Ted
[usersurname] => Smyth
[value] => 3688
[paid] => 2016-09-15 10:51:30
)

[1] => Array
(
[empcode] => 3422
[userfirstname] => Lynn
[usersurname] => Byrne
[value] => 346.47
[paid] => 2016-09-15 10:51:30
)

[2] => Array
(
[empcode] => 3436
[userfirstname] => Dave
[usersurname] => Smith
[value] => 233
[paid] => 2016-09-15 10:51:30
)

)


It must be the line

$output_arr[$k]["value"]=$foo["value"];


that is wrong.

I'd really appreciated any advice. Been banging my head off this since yesterday. TIA.

Answer

Here is a way to do it :

$rows = array();
foreach($temprows as $temprow){
  $empcode = $temprow['empcode'];

  if(!isset($rows[$empcode])){
    $rows[$empcode] = array(
      'empcode' => $empcode,
      'userfirstname' => $temprow['userfirstname'],
      'usersurname' => $temprow['usersurname'],
      'value' => 0,
      'paid' => date("Y-m-d H:i:s"),
    );
  }

  $rows[$empcode]['value'] += $temprow['value'];
}

Hope this helps.