如何从多维数组中删除重复值

时间:2013-03-29 00:01:27

标签: php arrays

我有一个像这样的数组:

Array
(
    [userId] => 35
    [fieldId] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 1
            [4] => 2
            [5] => 2
            [6] => 1
            [7] => 4
        )

    [educationTitle] => Array
        (
            [0] => School1
            [1] => School2
            [2] => 3
            [3] => School1
            [4] => School2
            [5] => School2
            [6] => School1
            [7] => 
        )
)

我想删除每个数组的所有重复项。所以,我希望最终的数组看起来像这样:

Array
(
    [userId] => 35
    [fieldId] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [7] => 4
        )

    [educationTitle] => Array
        (
            [0] => School1
            [1] => School2
            [2] => 3
            [7] => 
        )
)

我已尝试过此操作(按照此答案https://stackoverflow.com/a/307701/1009116中的建议):

  function multi_unique($updates) {
    foreach ($updates as $k=>$na)
        $new[$k] = serialize($na);
    $uniq = array_unique($new);
    foreach($uniq as $k=>$ser)
        $new1[$k] = unserialize($ser);
    return ($new1);
}

它没有效果

我也试过这个(按照这里的建议 - https://stackoverflow.com/a/946300/1009116

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

这只会返回最后一个数组(但是,它应该按原样进行过滤)

我做错了什么???

3 个答案:

答案 0 :(得分:3)

在这种情况下你可以使用array_combine();之后,您将数据拆分到各自的容器中。

$combined = array_combine($arr['fieldId'], $arr['educationTitle']);

$arr['fieldId'] = array_keys($combined);
$arr['educationTitle'] = array_values($combined);

请注意,此操作后原始索引会重新编号。

答案 1 :(得分:3)

您可以使用:

$data = array(
        'userId' => 35,
        'fieldId' => array(
                0 => 1,
                1 => 2,
                2 => 3,
                3 => 1,
                4 => 2,
                5 => 2,
                6 => 1,
                7 => 4
        ),
        'educationTitle' => array(
                0 => 'School1',
                1 => 'School2',
                2 => 3,
                3 => 'School1',
                4 => 'School2',
                5 => 'School2',
                6 => 'School1',
                7 => NULL
        )
);

print_r(arrayUnique($data));

输出

Array
(
    [userId] => 35
    [fieldId] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [7] => 4
        )

    [educationTitle] => Array
        (
            [0] => School1
            [1] => School2
            [2] => 3
            [7] => 
        )

)

使用的功能

function arrayUnique($array) {
    $input = array_map("unserialize", array_unique(array_map("serialize", $array)));
    foreach ( $input as $key => $value ) {
        is_array($value) and $input[$key] = arrayUnique($value);
    }
    return $input;
}

答案 2 :(得分:0)

使用array_flip();

的另一种方法
<?php
    $myArray = array( "userID"=>'35',
    "fieldID" => array(
    "0" => '1',"1" => '2',
    "2" => '3',"3" => '1',
    "4" => '2',"5" => '2',
    "6" => '1',"7" => '4'),
    "educationTitle"=>array(
    "0"=>'School1',"1"=>'School2',
    "2"=>'3',"3"=>'School1',
    "4"=>'School2',"5"=>'School2',
    "6"=>'School1',"7"=>'',),);

    print_r($myArray);

    $myArray['fieldID'] = array_flip($myArray['fieldID']);
    $myArray['fieldID'] = array_flip($myArray['fieldID']);
    $myArray['educationTitle'] = array_flip($myArray['educationTitle']);
    $myArray['educationTitle'] = array_flip($myArray['educationTitle']);

    print_r($myArray);
?>

最终输出

Array
(
    [userID] => 35
    [fieldID] => Array
        (
            [6] => 1
            [5] => 2
            [2] => 3
            [7] => 4
        )

    [educationTitle] => Array
        (
            [6] => School1
            [5] => School2
            [2] => 3
            [7] => 
        )

)