I want to leave duplicates inside my array and only delete one occurrence when a value is found more than once.
Given an array of:
$array = ['+5', '+5', '+3', '+3', '+3', '+3', '+5', '+5'];
+5 and +3 each occur four times. I want to remove just one of the +5 values and just one of the +3 values, then find the sum of the remaining values.
$duplicate = ['+5', '+3'];
$array = ['+5', '+5', '+3', '+3', '+3', '+3', '+5', '+5'];
$i = 0;
foreach ($duplicate as $dup) {
if (strpos($duplicate[$i], '+') !== false) {
$duplicate[$i] = preg_replace('/[^0-9,:]/', '', $duplicate[$i]);
$duplicate[$i] = "-$duplicate[$i]";
}
$i++;
}
$sum = array_merge($duplicate, $array);
$end_value = array_sum(array_values($sum));
var_export($end_value);
For my input, the final sum should be 24 (15 + 9).
the cards count them self. Please reinforce the required logic by presenting your exact desired result based on your sample input. Do you expect this? 3v4l.org/24GRdexplode()being called, but I don't see$expodebeing used anywhere. Are we working from a hyphen-delimited string ($player_cards[$i2])? If so, I can potentially remove a duplicate without iterated function calls. Please explain further.