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

时间:2022-08-26 14:54:45

I have an array of arrays like this:

我有这样的数组:

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] => 
        )
)

I want to remove all duplicates of each array. So, I want the final array to look like this:

我想删除每个数组的所有重复。我希望最终的数组是这样的

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

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

I've tried this (as recommended in this answer https://*.com/a/307701/1009116):

我已经尝试过了(按照这个答案的建议,https://*.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);
}

And it has no effect

它没有任何影响

I also tried this (as recommended here - https://*.com/a/946300/1009116)

我也尝试过这个(如这里推荐的- https://*.com/a/946300/1009116)

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

And this just returns the last array (however, it is filtered as it should be)

它只返回最后一个数组(但是,它被过滤了)

What am I doing wrong???

我做错了什么?

3 个解决方案

#1


3  

You can use :

您可以使用:

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

Output

输出

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

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

)

Function Used

函数使用

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


3  

You could use array_combine() in this case; later you pull apart the data into their respective containers.

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

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

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

Do note that the original indices are renumbered after this operation.

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

#3


0  

Another way using array_flip();

另一种方式使用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);
?>

Final Output

最终输出

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

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

)

#1


3  

You can use :

您可以使用:

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

Output

输出

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

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

)

Function Used

函数使用

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


3  

You could use array_combine() in this case; later you pull apart the data into their respective containers.

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

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

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

Do note that the original indices are renumbered after this operation.

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

#3


0  

Another way using array_flip();

另一种方式使用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);
?>

Final Output

最终输出

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

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

)