php array find duplicates, sum them up & delete duplicates
Just create a new array which you make fast adressable by using the setid as key. And reindex the array at the end.
$result = array();
foreach ($array as $val) {
if (!isset($result[$val['setid']]))
$result[$val['setid']] = $val;
else
$result[$val['setid']]['income'] += $val['income'];
}
$result = array_values($result); // reindex array
php remove duplicates and sum of them from array
try this, it will check that this key already exit if exit then sum value .if array stdClass Object then
$result = array();
foreach ($data as $val) {
if (!isset($result[$val->reservations_detail_article]))
$result[$val->reservations_detail_article] = $val;
else
$result[$val->reservations_detail_article]['reservations_detail_qte'] += $val['reservations_detail_qte'];
}
$result = array_values($result);
print_r($result);
if associative then
$result = array();
foreach ($data as $val) {
if (!isset($result[$val['reservations_detail_article']]))
$result[$val['reservations_detail_article']] = $val;
else
$result[$val['reservations_detail_article']]['reservations_detail_qte'] += $val['reservations_detail_qte'];
}
$result = array_values($result);
print_r($result);
How to duplicate array and sum up those array values in PHP
I recommend a linear approach with no preparations or padding or data bloat.
Use the indexes and simple arithmetic to add values to their desired element in the output.
Code: (Demo)
$array = range(1, 3);
$result = [];
foreach ($array as $shifter => $unused) {
foreach ($array as $index => $value) {
$key = $shifter + $index;
$result[$key] = ($result[$key] ?? 0) + $value;
}
}
var_export($result);
// [1, 3, 6, 5, 3]
This is very clean, readable, maintainable, and most efficient.
While $shifter = 0
, $key
will be 0
, 1
then 2
; forming [1, 2, 3]
.
While $shifter = 1
, $key
will be 1
, 2
then 3
; forming [1, 3, 5, 3]
.
While $shifter = 2
, $key
will be 2
, 3
then 4
; forming [1, 3, 6, 5, 3]
.
PHP sum array values if a specific column has a duplicate value
To SUM up all the values with the same FEE COLUMN you can create a for loop that iterates each result to check if the value is already in the array.
$data = Array(
Array
(
"type" => "Other Miscellaneous Fees",
"fee" => 158,
"amount" => -22.56,
"code" => "COL_AUDIO",
"feedesc" => "COLLEGE AUDIO VISUAL FEE"
),
Array(
"type" => "Other Miscellaneous Fees",
"fee" => 158,
"amount" => -297.86,
"code" => "COL_AUDIO",
"feedesc" => "COLLEGE AUDIO VISUAL FEE"
),
Array
(
"type" => "Other Miscellaneous Fees",
"fee" => 182,
"amount" => -40.00,
"code" => "STRAP",
"feedesc" => "ID STRAP"
),
Array
(
"type" => "Other Miscellaneous Fees",
"fee" => 177,
"amount" => -105.00,
"code" => "PRISAA",
"feedesc" => "PRISAA"
)
);
foreach ($data as $data_key => &$data_searcher) {
foreach ($data as $data_searcher_key => $data_searcher_value) {
if(
($data_searcher['fee'] === $data_searcher_value['fee']) &&
$data_key !== $data_searcher_key
){
$data_searcher['amount'] += $data_searcher_value['amount'];
unset($data[$data_searcher_key]);
}
}
}
echo "<pre>";
print_r($data);
echo "</pre>";
How to remove duplicate values from an array in PHP
Use array_unique().
Example:
$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
How to sum array value of duplicate data
@Cloud I have made function for your requirement and Thanks @M. I. for look into this sum section.
$array = array(
array("ID" => "126871","total"=>"200.00","currency"=>"USD","name"=>"John"),
array("ID" => "126872","total"=>"2000.00","currency"=>"Euro","name"=>"John"),
array("ID" => "126872","total"=>"1000.00","currency"=>"Euro","name"=>"John"),
array("ID" => "126872","total"=>"500.00","currency"=>"USD","name"=>"John"),
array("ID" => "126872","total"=>"1000.00","currency"=>"Euro","name"=>"John"),
);
echo "<pre>";
print_r($array);
function unique_multidim_array($array, $key,$key1,$addedKey) {
$temp_array = array();
$i = 0;
$key_array = array();
$key1_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array) && !in_array($val[$key1], $key1_array)) {
$key_array[$i] = $val[$key];
$key1_array[$i] = $val[$key1];
$temp_array[$i] = $val;
}else{
$pkey = array_search($val[$key],$key_array);
$pkey1 = array_search($val[$key1],$key1_array);
if($pkey==$pkey1){
$temp_array[$pkey][$addedKey] += $val[$addedKey];
}else{
$key_array[$i] = $val[$key];
$key1_array[$i] = $val[$key1];
$temp_array[$i] = $val;
}
// die;
}
$i++;
}
return $temp_array;
}
$nArray = unique_multidim_array($array,"ID","currency","total");
// die;
print_r($nArray);
die;
PHP sum array value based on textual duplicate in another array
Group by the "name" and sum as you iterate. When the loop is finished, split the keys and the values into separate arrays.
Code: (Demo)
$records = [
"Coding",
"Web development - Coding",
"Meeting",
"Coding",
"Coding",
"Content",
"Coding",
"Coding"
];
$quantities = [
3,
8,
1,
4.85,
1,
0.5,
6.01,
7
];
$result = [];
foreach ($records as $index => $label){
$result[$label] = ($result[$label] ?? 0) + $quantities[$index];
}
var_export(array_keys($result));
var_export(array_values($result));
Outputs:
array (
0 => 'Coding',
1 => 'Web development - Coding',
2 => 'Meeting',
3 => 'Content',
)
array (
0 => 21.86,
1 => 8,
2 => 1,
3 => 0.5,
)
Related Topics
PHP Mail Special Characters Utf8
Using Regex to Filter Attributes in Xpath with PHP
Mysqli_Stmt::Execute() Expects Exactly 0 Parameters, 1 Given
Resetting MySQL Root Password with Xampp on Localhost
Codeigniter Activerecord, Retrieve Last Insert Id
How to Set a Class Attribute to a Symfony2 Form Input
Php: Split String into Array, Like Explode with No Delimiter
What Is the Syntax for Sorting an Eloquent Collection by Multiple Columns
PHP Linefeeds (\N) Not Working
PHP Array Find Duplicates, Sum Them Up & Delete Duplicates
How to Put JavaScript Variable in PHP Echo
Codeigniter Sessions Not Working After Migration
Multiple Excerpt Lengths in Wordpress
Why, Fatal Error: Class 'Phpunit_Framework_Testcase' Not Found in ...