I would break this into three steps.
Step One: transform this
[0] => Array
(
[name] => SMITH
[status] => Incomplete
[count] => 2
)
[1] => Array
(
[name] => SMITH
[status] => Complete
[count] => 2
)
into this
[0] => Array
(
[name] => SMITH
[Incomplete] => 2
)
[1] => Array
(
[name] => SMITH
[Complete] => 2
)
which can be done by the following
$data = array_map($data, function($d) {
return array('name' => $d['name'], $d['status'] => $d['count']);
});
Step Two:
I have seen many people ask about this stage of problem, typically when dealing with database query result sets. The basic problem is that instead of indexing the records by row number, you want to index the records based on some data each record contains. So, here is a generic function...
function make_index($array, $key) {
foreach ($array as $a) {
$r = &$reindexed[$a[$key]];
$r = array_merge((array)$r, $a);
}
return $reindexed;
}
acting on our result from Step One, presuming we call it as $data = make_index($data, "name");
, we will arrive at
Array
(
[SMITH] => Array
(
[name] => SMITH
[Incomplete] => 2
[Complete] => 2
)
)
Step Three:
now you want to go back to numerical indexes, and that is easy achieved.
$data = array_values($data);
gives
Array
(
[0] => Array
(
[name] => SMITH
[Incomplete] => 2
[Complete] => 2
)
)
Conclusion:
It is useful to break data set transformations into baby steps. By doing so, you can reuse individual steps in different problems. Also, for complex changes, it becomes easier to arrive at a solution, or understand the code once it is written.
A Note about Step Two:
In step two I provided a "generic" function, but really, the merge done by array_merge will not accommodate most situations. Specifically, query results have records containing all the same keys, and a simple overwrite is not likely the aggregation method desired. Here is another variant to overcome this limitation.
function make_index($array, $key, $M) {
foreach ($array as $a) {
$r = &$reindexed[$a[$key]];
$r = $M((array)$r, $a);
}
return $reindexed;
}
// ex: $data = make_index($data, "name", function($e1, $e2){
// return array_merge($e1, $e2);
// });
// equiv to: $data = make_index($data, "name", 'array_merge');