2

我有一个这样的数组数组:

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

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

我究竟做错了什么???

4

3 回答 3

3

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

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

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

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

于 2013-03-29T00:06:26.120 回答
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;
}
于 2013-03-29T00:07:07.830 回答
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] => 
        )

)
于 2013-03-29T00:58:14.407 回答