PHP在数组中找到相同的记录

时间:2016-07-19 13:25:30

标签: php arrays sorting array-difference

我想检测相同的记录,然后更改一条记录的数量并删除其他记录。例如,给定以下数组:

'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
    ]
],

我想打印:

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

],

6 个答案:

答案 0 :(得分:1)

在我看来,您试图在重复尺寸(尺寸)和代码(代码)上对数量(qn)求和。实现此目的的一种方法是循环包含购物车项目的数组并构建一个新数组。我建议您阅读有关PHP arraysarray_key_exists的内容,以便了解以下内容:

<?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);

答案 1 :(得分:0)

我不知道这对你的问题是否足够,但试试函数array_unique

答案 2 :(得分:0)

迭代您的数组并在每个子数组上使用array_diff。 如果返回的数组既不包含size也不包含code,请添加两个qn

答案 3 :(得分:0)

函数array_unique()适用于单维数组。要从多维数组中查找唯一元素,我们需要做一些修改。

试试这个:

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

如果您想要采用其他方法,请将其作为参考:Create multidimensional array unique

答案 4 :(得分:0)

请仔细阅读以下代码。

    $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);

答案 5 :(得分:0)

代码针对您的问题进行了测试,我已经解释了代码。这为您提供了任意数量的数组的解决方案,并且具有数量(qn)的类似数组元素,可以根据需要增加:

<?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) { }?>