获取唯一阵列

时间:2012-04-24 17:34:38

标签: php arrays sorting indexing unique

我有一个数组

    Array
    (
     [fbid] = Array
            (
                **[0] => 115637705237540
                [1] => 115637705237540**
                [2] => 111723238962320
                [3] => 111723248962319
                [4] => 112721842195793
                [5] => 112567698877874
                [6] => 111844022283575
                **[7] => 115637705237540**
                [8] => 111840252283952
                [9] => 109642909170353
            )

       [fb_parentid] = Array
            (
                [0] => 571228125
                [1] => 571228125
                [2] => 571228125
                [3] => 571228125
                [4] => 571228125
                [5] => 571228125
                [6] => 571228125
                [7] => 571228125
                [8] => 571228125
                [9] => 571228125
            )

)

这里重复0,1和7。首先,我想获得重复索引(0,1,7)。其次,我想显示一个索引。在上面的例子中只有第0个。

之后我想从[fb_parentid]和[fbid]中删除1和7

你能帮助我吗?

2 个答案:

答案 0 :(得分:6)

您正在寻找array_unique功能。

$new_array = array_unique($old_array);

答案 1 :(得分:0)

尝试

$array = Array
    (
     "fbid" => Array
            (
                "0" => 115637705237540,
                "1" => 115637705237540,
                "2" => 111723238962320,
                "3" => 111723248962319,
                "4" => 112721842195793,
                "5" => 112567698877874,
                "6" => 111844022283575,
                "7" => 115637705237540,
                "8" => 111840252283952,
                "9" => 109642909170353
            ),

       "fb_parentid" => Array
            (
                "0" => 571228125,
                "1" => 571228125,
                "2" => 571228125,
                "3" => 571228125,
                "4" => 571228125,
                "5" => 571228125,
                "6" => 571228125,
                "7" => 571228125,
                "8" => 571228125,
                "9" => 571228125
            )

);



$unique = array_unique($array["fbid"]);
$diffrence = array_diff_assoc($array["fbid"], $unique) ; // Return the difference 

var_dump($diffrence); 


$unique = array_unique($array["fb_parentid"]); // Get Unique Values 

var_dump($unique);

输出

array
1 => float 1.1563770523754E+14
7 => float 1.1563770523754E+14
array
0 => int 571228125