add array element if get duplicate element in php
$a=[3.00,3.00,8.00,3.00,13.00]
$b=[0.11,0.45,0.22,0.90,0.44]
the result should be:
$c=[3.00,8.00,13.00]
$d=[1.46(0.11+0.45+0.90),0.22,0.44]
Just as I don't like having the added isset logic, I create an array of the unique keys, then create a start array with 0 values for the sum values. Then just match the initial array against the unique array and add the value in...
$a=[3.00,3.00,8.00,3.00,13.00];
$b=[0.11,0.45,0.22,0.90,0.44];
$c = array_values(array_unique($a));
$d = array_fill(0, count($c), 0);
foreach ( $a as $key => $value ) {
$d[ array_search($value, $c) ] += $b[$key];
}
print_r($c);
print_r($d);
<?php
$a = [3.00, 3.00, 8.00, 3.00, 13.00];
$b = [0.11, 0.45, 0.22, 0.90, 0.44];
$c = array();
$d = array();
foreach ($a as $index => $per_a) {
if (!in_array($per_a, $c)) {
array_push($c, $per_a);
array_push($d, $b[$index]);
}
else {
$pos = array_search($b[$index], $d);
$d[$pos] += $b[$index];
}
}
print_r($c);
print_r($d);
1.46here? Something like weight of every number?1.46*1.46, the others just0.22&0.44not0.22*0.22,0.44*0.44?