如何从php中的自定义数组中删除重复记录

时间:2014-01-30 07:58:08

标签: php arrays

我尝试使用php从我自定义生成的数组中删除重复记录。但我得到相同的数组.Below是输入数组,过滤后需要数组。

INPUT ARRAY:

Array
(
    [0] => Array
        (
            [CSG] => Array
                (
                    [group_id] => 1111
                    [group_name] => GRP NAME 1
                    [status] => 1
                )

            [TFE] => Array
                (
                    [track_fb_export_id] => 
                )

        )

    [1] => Array
        (
            [CSG] => Array
                (
                    [group_id] => 2222
                    [group_name] => GRP NAME 2
                    [status] => 1
                )

            [TFE] => Array
                (
                    [track_fb_export_id] => 
                )

        )

    [2] => Array
        (
            [CSG] => Array
                (
                    [group_id] => 2222
                    [group_name] => GRP NAME 2
                    [status] => 1
                )

            [TFE] => Array
                (
                    [track_fb_export_id] => 
                )

        )

)

预期结果是什么:

Array
    (
        [0] => Array
            (
                [CSG] => Array
                    (
                        [group_id] => 1111
                        [group_name] => GRP NAME 1
                        [status] => 1
                    )

                [TFE] => Array
                    (
                        [track_fb_export_id] => 
                    )

            )

        [1] => Array
            (
                [CSG] => Array
                    (
                        [group_id] => 2222
                        [group_name] => GRP NAME 2
                        [status] => 1
                    )

                [TFE] => Array
                    (
                        [track_fb_export_id] => 
                    )

            )

    )

带有group_id的GRP NAME 2是重复的,因此想要从数组中删除重复的组。

我试过的代码:

$unique = array_map("unserialize", array_unique(array_map("serialize", $input)));

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

这对你有用     

//data array
$data = array();

//group id collector
$group_id = array();

//the index won't change now 
foreach($input as $key=>$value)
{
    if(!in_array($value['CSG']['group_id'], $group_id))
    {
        $group_id[] = $value['CSG']['group_id'];
        $data[] = $value;
    }

}

//check the data
print_r($data);

答案 1 :(得分:0)

几步:

  • 首先,您必须对有关group_name
  • 的数组进行排序
function cmp($a, $b) {
    $sa = $a['CSG']['group_name'];
    $sb = $b['CSG']['group_name'];
    $compare = strcmp($a, $b);

    if($compare > 0)
        return 1;
    else if($compare < 0) 
        return -1;
    else
        return 0;
}

uasort($array, 'cmp');

函数uasort采用一个数组和一个函数来调用数组。

  • 然后移动你的阵列并制作另一个阵列。您可以保留新数组中添加的最后一个条目,并将其与当前条目进行比较,这要归功于您之前的排序。
$newArray = array();
$lastArray = null;

foreach($a as $array) {
    if($lastArray == null || $a['CSG']['group_name'] != $lastArray['CSG']['group_name']) {
        $lastArray = $a;
        $newArray[] = $a;
    }
}