I would appreciate some help regarding the processing of my below multidimensional array data. What I want to achieve is to build an array with duplicated dates as keys and the total amount in "nb" indexes for each duplicated date. This is what I've done so far:
<?php
$caOppo = [
0 => [
"nb" => "5000",
"date" => "2016-01-26",
"nb_enc" => "1",
"tp" => "BUSINESS PB"
],
1 =>[
"nb" => "9100",
"date" => "2016-01-26",
"nb_enc" => "1",
"tp" => "BLUE SOMETHING"
],
2 => [
"nb" => "12000",
"date" => "2016-01-12",
"nb_enc" => "1",
"tp" => "BUSINESS MC"
],
3 => [
"nb" => "20000",
"date" => "2016-01-22",
"nb_enc" => "1",
"tp" => "BUSINESS MC"
],
4 => [
"nb" => "29900",
"date" => "2016-01-25",
"nb_enc" => "1",
"tp" => "KLIPAY"
],
5 => [
"nb" => "36350",
"date" => "2016-01-04",
"nb_enc" => "2",
"tp" => "KLIPAY"
],
6 => [
"nb" => "36900",
"date" => "2016-01-27",
"nb_enc" => "1",
"tp" => "BUSINESS PB"
],
7 => [
"nb" => "45400",
"date" => "2016-01-13",
"nb_enc" => "1",
"tp" => "BUSINESS MC"
],
8 => [
"nb" => "50700",
"date" => "2016-01-08",
"nb_enc" => "3",
"tp" => "BUSINESS MC"
],
9 => [
"nb" => "54000",
"date" => "2016-01-07",
"nb_enc" => "1",
"tp" => "BLUE SOMETHING"
],
10 => [
"nb" => "61750",
"date" => "2016-01-26",
"nb_enc" => "3",
"tp" => "BUSINESS MC"
],
11 => [
"nb" => "78900",
"date" => "2016-01-15",
"nb_enc" => "1",
"tp" => "KLIPAY"
],
12 => [
"nb" => "171050",
"date" => "2016-01-20",
"nb_enc" => "8",
"tpe_oppo" => "BUSINESS MC"
],
13 => [
"nb" => "172900",
"date" => "2016-01-19",
"nb_enc" => "7",
"tp" => "BUSINESS MC"
],
14 => [
"nb" => "208950",
"date" => "2016-01-22",
"nb_enc" => "10",
"tp" => "BUSINESS PB"
]
];
$dates = [];
foreach ($caOppo as $uniqueCaOppo) {
$dates[] = $uniqueCaOppo['date'];
}
//Retrieving duplicates
$duplicates = [];
foreach(array_count_values($dates) as $val => $c){
if($c > 1){
$duplicates[$val] = 0;
}
}
print_r($duplicates); // Returns Array ( [2016-01-26] => 0 [2016-01-22] => 0 )
//which is what is expected
For now the amount for each dupilicated is set to 0. Now we need to add up all the $caOppo[$i]['nb'] for each duplicated date, the result will replace 0.
for ($i=0; $i < count($caOppo); $i++) {
if (in_array($caOppo[$i]['date'], array_keys($duplicates))) {
foreach ($duplicates as $key => $value) {
if($caOppo[$i]['date']= $key){
$duplicates[$key] += $caOppo[$i]['nb'];
}
}
}
}
print_r($duplicates); // Returns Array ( [2016-01-26] => 304800 [2016-01-22]
//=> 304800 ), which is not what is expected
Thanks in advance for your help.
Also wondering if there is a way to transform all the multidimensional array so duplicated dates will be replaced by only one instance of that date containing the sum total in $caOppo[$i]['nb'].
$cnt=count($caOppo);
for ($i=0;$i<$cnt;$i++) {
$caOppo[$i]['nb']=(int)$caOppo[$i]['nb'];
for ($j=0;$j<$i;$j++) {
if (isset($caOppo[$j])&&($caOppo[$j]['date']===$caOppo[$i]
['date'])) {
$caOppo[$j]['nb']+=$caOppo[$i]['nb'];
$caOppo[$j]['tp']=[];
$tp_value = $caOppo[$i]['tp'];
$caOppo[$j]['tp'][$tp_value]=$caOppo[$i]['nb'];
unset($caOppo[$i]);
break;
}
}
}