I have two arrays which have items and quantities and they are asociated by the index of the arrays. Example:
Items=XL,M,XL,S
Quantities=5,2,4,7
What I want to achieve is to remove the repetitive items and sum their quantities:
Items=XL,M,S
Quantities=9,2,7
$uniqueTallesTotalesC = array_unique($tallesTotalesC);
for($i=0;$i<Count($uniqueTallesTotalesC);$i++){
for($e=0;$e<Count($cantidadesTotalesC);$e++){
if($uniqueTallesTotalesC[$i]==$tallesTotalesC[$e] && $e > $i){
$cantidadesTotalesC[$i] = $cantidadesTotalesC[$i] + $cantidadesTotalesC[$e];
} else{
}
}
}
2
Answers
You can just blindly zip-merge them together with summation:
Outputs:
Demo: https://3v4l.org/6A5ZC
There’s probably an autovivification that I’m missing that could simplify this greater.
I suggest you to create another array variable to hold the new items’ quantities:
Hope it helps!