<?php
$array = [
[
'tag_id' => "6291",
'az' => 5,
],
[
'tag_id' => "6291",
'az' => 4,
],
[
'tag_id' => "6311",
'az' => 4,
],
[
'tag_id' => "6427",
'az' => 4,
]
];
$tag_id_indexes = [];
array_walk(
$array,
function ($sub_array, $index) use (&$array, &$tag_id_indexes) {
if (!isset($tag_id_indexes[$sub_array['tag_id']])) {
$tag_id_indexes[$sub_array['tag_id']] = $index;
}
else {
$first_tag_id_index = $tag_id_indexes[$sub_array['tag_id']];
$array[$first_tag_id_index]['az'] += $sub_array['az'];
unset($array[$index]);
}
}
);
print "The reduced array but with the original indexes:
" . var_export($array, true) . "
";
$array = array_values($array);
print "The reduced array with new indexes:
" . var_export($array, true) . "
";