Reputation: 21
This is my array i want to check one index value to remain other index value and if product_id value same then sum of total_qty. i am stuck in this situation.i need help.Thank you in advance.
Array
(
[0] => Array
(
[iProduct_id] => 1
[fTotal_qty] => 200
)
[1] => Array
(
[iProduct_id] => 2
[fTotal_qty] => 400
)
[2] => Array
(
[iProduct_id] => 6
[fTotal_qty] => 500
)
[3] => Array
(
[iProduct_id] => 4
[fTotal_qty] => 300
)
[4] => Array
(
[iProduct_id] => 5
[fTotal_qty] => 200
)
[5] => Array
(
[iProduct_id] => 6
[fTotal_qty] => 200
)
[6] => Array
(
[iProduct_id] => 1
[fTotal_qty] => 300
)
)
i want output like this
product_id = 1
total_qty = 500
product_id = 2
total_qty = 400
product_id = 6
total_qty = 700
product_id = 4
total_qty = 300
product_id = 5
total_qty = 200
Upvotes: 0
Views: 61
Reputation: 516
Try this for full details
<?php
$array = array(array("iProduct_id" => "1", "fTotal_qty" => 200), array("iProduct_id" => "2", "fTotal_qty" => 400), array("iProduct_id" => "1", "fTotal_qty" => 100), array("iProduct_id" => "2", "fTotal_qty" => 600));
$res = array();
foreach($array as $vals){
if(array_key_exists($vals['iProduct_id'],$res)){
$res[$vals['iProduct_id']]['fTotal_qty'] += $vals['fTotal_qty'];
$res[$vals['iProduct_id']]['iProduct_id'] = $vals['iProduct_id'];
}
else{
$res[$vals['iProduct_id']] = $vals;
}
}
foreach ($res as $val) {
echo 'product_id = ' . $val['iProduct_id'].'<br>';
echo 'total_qty = ' . $val['fTotal_qty'].'<br>' ;
}
?>
Output
product_id = 1
total_qty = 300
product_id = 2
total_qty = 1000
Upvotes: 0
Reputation: 681
Why not use PHP Sorting functions that do the job optimizing the sort. In PHP 7 you can use:
$array = array (
0 => array ("iProduct_id" => 1,"fTotal_qty" => 200),
1 => array ("iProduct_id" => 2,"fTotal_qty" => 400),
2 => array ("iProduct_id" => 6, "fTotal_qty" => 500),
3 => array ("iProduct_id" => 4, "fTotal_qty" => 300),
4 => array ("iProduct_id" => 5, "fTotal_qty" => 200),
5 => array ("iProduct_id" => 6, "fTotal_qty" => 200),
6 => array ("iProduct_id" => 1, "fTotal_qty" => 300)
);
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
usort($array, function ($item1, $item2) {
return $item2['fTotal_qty'] <=> $item1['fTotal_qty'];
});
echo(print_r($array,1));
Upvotes: 0
Reputation: 3733
<?php
$array = [
'0' =>
[
'iProduct_id' => 1,
'fTotal_qty' => 200
],
'1' =>
[
'iProduct_id' => 2,
'fTotal_qty' => 400
],
'2' =>
[
'iProduct_id' => 6,
'fTotal_qty' => 500
],
'3' =>
[
'iProduct_id' => 4,
'fTotal_qty' => 300
],
'4' =>
[
'iProduct_id' => 5,
'fTotal_qty' => 200
],
'5' =>
[
'iProduct_id' => 6,
'fTotal_qty' => 200
],
'6' =>
[
'iProduct_id' => 1,
'fTotal_qty' => 300
]
];
$result = [];
foreach ($array as $key => $value){
if (array_key_exists($value['iProduct_id'], $result)) {
$result[$value['iProduct_id']] += $value['fTotal_qty'];
} else {
$result[$value['iProduct_id']] = $value['fTotal_qty'];
}
}
foreach ($result as $k => $v) {
echo 'product_id = ' . $k . PHP_EOL;
echo 'total_qty = ' . $v . PHP_EOL;
}
Result:
product_id = 1
total_qty = 500
product_id = 2
total_qty = 400
product_id = 6
total_qty = 700
product_id = 4
total_qty = 300
product_id = 5
total_qty = 200
Upvotes: 0
Reputation: 895
With the below code you can get the result you are expecting.
<?php
$array = Array
( Array
(
"iProduct_id" => "1",
"fTotal_qty" => "200",
),
Array
(
"iProduct_id" => "2",
"fTotal_qty" => "400",
),
Array
(
"iProduct_id" => "6",
"fTotal_qty" => "500",
),
Array
(
"iProduct_id" => "4",
"fTotal_qty" => "300",
),
Array
(
"iProduct_id" => "5",
"fTotal_qty" => "200",
),
Array
(
"iProduct_id" => "6",
"fTotal_qty" => "200",
),
Array
(
"iProduct_id" => "1",
"fTotal_qty" => "300",
)
);
$resultArray = array();
foreach($array as $key => $value){
if(array_key_exists($value['iProduct_id'], $resultArray)){
$resultArray[$value['iProduct_id']]['total_qty'] = $resultArray[$value['iProduct_id']]['total_qty'] + $value['fTotal_qty'];
}else {
$resultArray[$value['iProduct_id']]['product_id'] = $value['iProduct_id'];
$resultArray[$value['iProduct_id']]['total_qty'] = $value['fTotal_qty'];
}
}
Upvotes: 0
Reputation: 11642
You can do this with simple for-loop
$arr = array(array("id" => "1", "amount" => 200), array("id" => "2", "amount" => 400), array("id" => "1", "amount" => 100), array("id" => "2", "amount" => 600));
$res = array();
foreach($arr as $elem) {
if (!isset($res[$elem["id"]])) //if this is the first time for this ID
$res[$elem["id"]] = 0;
$res[$elem["id"]] += $elem["amount"]; // add current amount
}
Now res
contains (key is the product_id
and the value is the total_qty
):
Array
(
[1] => 300
[2] => 1000
)
Upvotes: 1