-3

I would like to detect same records and then change quantity of the one record and delete the others. For example, given the following array:

'Cart' => [
    (int) 0 => [
        'size' => '38',
        'code' => '5',
        'qn' => (int) 1
    ],
    (int) 1 => [
        'size' => '37',
        'code' => '5',
        'qn' => (int) 1
    ],
    (int) 2 => [
        'size' => '37',
        'code' => '5',
        'qn' => (int) 1
    ]
],

i would like to print:

'Cart' => [
    (int) 0 => [
        'size' => '38',
        'code' => '5',
        'qn' => (int) 1
    ],
    (int) 1 => [
        'size' => '37',
        'code' => '5',
        'qn' => (int) 2
    ]

],
floriank
  • 25,546
  • 9
  • 42
  • 66

6 Answers6

1

It looks to me that you're trying to sum quantities (qn) on duplicate sizes (size) and codes (code). One way to achieve this is by looping through the array containing the cart items and building out a new array. I suggest reading about PHP arrays and array_key_exists to learn more as they're used below:

<?php

$arr = [
    ['size' => '38', 'code' => 5, 'qn' => 1],
    ['size' => '37', 'code' => 5, 'qn' => 1],
    ['size' => '37', 'code' => 5, 'qn' => 1],
    ['size' => '37', 'code' => 4, 'qn' => 1],
];

$newArr = [];

foreach ($arr as $value) {
    $key = $value['size'] . ':' . $value['code'];

    if (array_key_exists($key, $newArr)) {
        $newArr[$key]['qn'] += $value['qn'];
        continue;
    }

    $newArr[$key] = $value;
}

// Resets keys
$newArr = array_values($newArr);

print_r($newArr);
  • Your are checking the similar size only. But have to check Size and Code both and then have to increase the qn and remove one record. – Rahul Patel Jul 19 '16 at 13:58
0

I dont know it is enough for your problem, but try function array_unique

Marcin Pruciak
  • 336
  • 1
  • 3
  • 14
  • Array_unique will delete duplicate data. I need to count how many there is alike records and than update quantity. Look, qn in secod array changes. – Łukasz Skup Jul 19 '16 at 13:47
0

Iterate your array and use array_diff on each subarray. If the returned array doesn't contain neither size nor code, add the two qn

jeanj
  • 2,106
  • 13
  • 22
0

The function array_unique() works for single dimension array. To find unique elements from multi-dimension array, we need to do a little modification.

Try this:

$array  = array_map("serialize", $array);
$output = array_map("unserialize", array_unique($array));

If you want to follow another approach, use this as a reference: Create multidimensional array unique

Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
0

Please go through following code.

    $result = [
        0=> ['size' => '38',
                'code' => '5',
                'qn' => 1
            ],
        1=> ['size' => '37',
               'code' => '5',
               'qn' => 1
            ],
        2=> ['size' => '37',
                'code' => '5',
                'qn' => 1
            ]
    ];

$finalArr = [];
foreach($result as $k => $v) {
    $flag = 0;
    foreach($finalArr as $kc => $vc){
        if($v['size']==$vc['size'] && $v['code']==$vc['code']){
            $flag = 1;  
            $finalArr[$kc]['qn']    = $finalArr[$kc]['qn'] + 1;
            break;
        } 
    }
    if($flag==0){
        $finalArr[] = 
        [
            'size'  => $v['size'],
            'code'  => $v['code'],
            'qn'    => 1
        ];
    }       
}
echo "<pre>";
print_r($finalArr);
Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
0

The code is tested against your question and i have explained the code. This gives you solution for any number of arrays and with similar array elements with quantity(qn) incremented as you wanted:

<?php
//Compare the 1st and 2nd array
//Compare the 2st and 3rd array
// and so on
function compare($arr1 = array(), $arr2 = array()) {        
    $result = array_diff($arr1, $arr2);  
    return $result;
}

$cart = array(
        0 => array('size' => 38, 'code' => 5, 'qn' => 1),
        1 => array('size' => 37, 'code' => 5, 'qn' => 1),
        2 => array('size' => 37, 'code' => 5, 'qn' => 1),
        );
$j = 1;
$cart_total_count = count($cart);//Gives the count of your cart
$final_cart = array();//Final array with qn incremented
for($i=0;$i<$cart_total_count; $i++) {
    if (!empty($cart[$i+1])) {//If 2nd array is not present then stop the comparision
        if (empty(compare($cart[$i], $cart[$i+1]))){
            $j++;
            $cart[$i]['qn'] = $j;                      
            $final_cart = $cart[$i];            
        }
    }
}var_dump($final_cart);
//Output 
//1. If 2 array are same
//array(3) { ["size"]=> int(37) ["code"]=> int(5) ["qn"]=> int(2) }
//2. If no array are same
//array(0) { }?>
Ravistm
  • 2,163
  • 25
  • 25