I've been digging around and trying to solve this issue the cleanest way possible, but haven't quite found the right approach.
I have an array of objects like so:
$myArray = [
{field: "Diameter", measurement: 15, count: 4},
{field: "Diameter", measurement: 16, count: 1},
{field: "Diameter", measurement: 17, count: 15},
{field: "Width", measurement: 7, count: 12},
{field: "Width", measurement: 8, count: 8},
{field: "Brands", measurement: "blah", count: 1},
{field: "Brands", measurement: "doubleBlah", count: 3},
{field: "Brands", measurement: "blah", count: 1},
{field: "Brands", measurement: "doubleBlah", count: 3},
{field: "Brands", measurement: "blah", count: 12}
]
and I need to combine the objects that have duplicate measurement fields and combine the counts like this:
$myBetterArray = [
{field: "Diameter", measurement: 15, count: 4},
{field: "Diameter", measurement: 16, count: 1},
{field: "Diameter", measurement: 17, count: 15},
{field: "Width", measurement: 7, count: 12},
{field: "Width", measurement: 8, count: 8},
{field: "Brands", measurement: "blah", count: 14},
{field: "Brands", measurement: "doubleBlah", count: 6},
]
can it be done with something like array_map or something equally clean and not too verbose? Any help would be much appreciated.