在数组中查找唯一值

时间:2012-02-17 14:07:33

标签: php arrays

我有一个类似下面的数组:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 2
                    [1] => 0
                )

            [1] => Array
                (
                    [0] => 3
                    [1] => 1
                )

    [1] => Array
        (
            [0] => Array 
                (
                   [0] => 3
                   [1] => 1
                )

            [1] => Array 
                (
                   [0] => 4
                   [1] => 2
                )

            [2] => Array 
                (
                   [0] => 5
                   [1] => 2
                )
        )
    [2] => Array
        (
            [0] => Array
                (
                    [0] => 2
                    [1] => 1
                )

            [1] => Array
                (
                    [0] => 5
                    [1] => 2
                )
        )

...

)

我想删除重复的字段,并在构成“大数组”的每个array()中包含唯一值。

例如:$array[0]位于值(3,1) ($array[0][1])内,此值(2,0)也位于$array[1][0]中,因此我想删除它们。

我在上面发布的数组应该在该过程之后返回以下数组

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 2
                    [1] => 0
                )


    [1] => Array
        (

            [1] => Array 
                (
                   [0] => 4
                   [1] => 2
                )
        )
    [2] => Array
        (
            [0] => Array
                (
                    [0] => 2
                    [1] => 1
                )
        )

...

)

是否有任何有用的功能有帮助?我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

我不认为这有一个PHP功能。但你可以尝试这样的事情:

$array = array(array(array(2,0),array(3,1)),array(array(3,1),array(4,2),array(5,2)),array(array(2,1),array(5,2)));


$newArray = array();

foreach($array as $innerArray){                 //Write all the innermost arrays into one array
    foreach($innerArray as $innerMostArray){
        $newArray[] = $innerMostArray;
    }
}

$uniqueArrays = array();
$duplicateArrays = array();

foreach($newArray as $key => $innerArray){          //Search for unique arrays in $newArray
    unset($newArray[$key]);

    if(!in_array($innerArray,$duplicateArrays)){ //If array is already in duplicate arrays, just ignore it
        if(in_array($innerArray,$newArray)){
            $duplicateArrays[] = $innerArray;
        }else{
            $uniqueArrays[] = $innerArray;
        }
    }
}

unset($duplicateArrays);
unset($newArray);

$newArray = array();

foreach($array as $innerArray){ //Rebuild the structure of the old array
    $newInnerArray = array();

    foreach($uniqueArrays as $uniqueArray){
        $key = array_search($uniqueArray,$innerArray);

        if($key !== false){ //Only write the unique arrays into the new array
            $newInnerArray[$key] = $uniqueArray; //preserve the old key
            unset($uniqueArrays[$key]);
        }
    }

    if(count($newInnerArray) != 0){
        $newArray[] = $newInnerArray;
    }
}

var_dump($newArray);

希望这有帮助。

答案 1 :(得分:0)

看一下array_unique()函数..它会指出每个函数中的唯一函数。

或其他

array_diff()另一个看到差异