I have an array in the following format:
array(5){
[0] =>
array(4){
['product-id'] => 8931
['product'] => 'Cake'
['description'] => 'Yellow cake'
['quantity'] => 1
}
[1] =>
array(4){
['product-id'] => 8921
['product'] => 'Cookies'
['description'] => 'Chocolate chip cookies'
['quantity'] => 2
}
[2] =>
array(4){
['product-id'] => 8931
['product'] => 'Cake'
['description'] => 'Yellow cake'
['quantity'] => 1
}
[3] =>
array(4){
['product-id'] => 8931
['product'] => 'Cake'
['description'] => 'Yellow cake'
['quantity'] => 4
}
[4] =>
array(4){
['product-id'] => 8933
['product'] => 'Cake'
['description'] => 'Chocolate cake'
['quantity'] => 1
}
}
How can I compare all the arrays to each other? In my code I have sorted all the arrays by product ID, and written a for to compare two rows at a time, but now I see why that won't work.
function cmp($a, $b){
return strcmp($a[0], $b[0]);
}
function combineArrays($arrays){
usort($arrays, "cmp");
for($i = 1; $i < count($arrays); $i++){
$f_row = $arrays[$i];
$next = $i + 1;
$s_row = $arrays[$next];
if($f_row[0] == $s_row[0]){
if($f_row[1] == $s_row[1]){
if($f_row[2] == $s_row[2]){
$q1 = (int) $f_row[3];
$q2 = (int) $s_row[3];
$total = $q1 + $q2;
unset($f_row[3]);
$f_row[5] = $total;
unset($arrays[$next]);
}
}
}
}
return $arrays;
}
What is a better way to do this?
For example, to take the first array and compare it to the next, value for value. As soon as one of the first 3 values doesn't match, you go on to compare that row to the next one. If all of the first three values match, add up the quantity value of the two arrays, assign that to the first array's quantity, and get rid of the second array. There could be more matches, so continue comparing that array until you have gone through the whole list.
My favorite solution uses array_reduce()
:
$filtered = array_reduce(
// Reduce the original list
$arrays,
// The callback function adds $item to $carry (the partial result)
function (array $carry, array $item) {
// Generate a key that contains the first 3 properties
$key = $item['product-id'].'|'.$item['product'].'|'.$item['description'];
// Search into the partial list generated until now
if (array_key_exists($key, $carry)) {
// Update the existing item
$carry[$key]['quantity'] += $item['quantity'];
} else {
// Add the new item
$carry[$key] = $item;
}
// The array_reduce() callback must return the updated $carry
return $carry;
},
// Start with an empty list
array()
);
You are doing it the hard way, why not do it like this:
function combineProducts($products) {
$result = array();
foreach ($products as $product) {
//if you can have different product or descriptions
//per product id you can change this this to
//$productId = implode('|', array($product['product-id'], $product['product'], $product['description']);
$productId = $product['product-id'];
//check if we already have this product
if (isset($result[$productId])) {
//add to the quantity
$result[$productId]['quantity']+= $product['quantity'];
} else {
$result[$productId] = $product;
}
}
//sort the results (remove if not needed)
ksort($result);
//return values (change to return $result; if you want an assoc array)
return array_values($result);
}
try this
$arr=array(
array(
'product-id' => 8931,
'product' => 'Cake',
'description' => 'Yellow cake',
'quantity' => 3,
),
array(
'product-id' => 8921,
'product' => 'Cookies',
'description' => 'Chocolate chip cookies',
'quantity' => 2,
),
array(
'product-id' => 8931,
'product' => 'Cake',
'description' => 'Yellow cake',
'quantity' => 1,
)
);
$id = array();
foreach ($arr as $key => $row)
{
$id[$key] = $row['product-id'];
}
array_multisort($id, SORT_DESC, $arr);
$result = array();
foreach ($arr as $key => $row)
{
$pid = $row['product-id'];
if(!isset($result[$pid]))
{
$result[$pid]=$row;
}else{
$result[$pid]['quantity']+=$row['quantity'];
}
}
print_r($result);