从多维数组中删除重复值

时间:2014-07-21 11:03:21

标签: php arrays multidimensional-array

任何人都可以解释如何将下面的输入数组更改为输出数组

输入

Array
(
 [24] => Array
    (
        [0] => Moto E
        [1] => Moto G
    )

[23] => Array
    (
        [0] => Moto  G
    )

[22] => Array
    (
        [0] => Nokia
        [1] => Karbon
        [2] => onida
        [3] => micromax
        [4] => L'oreal
        [5] => 
    )

[21] => Array
    (
        [0] => brand1
        [1] => brand2
    )

[20] => Array
    (
        [0] => Nokia
        [1] => Apple
        [2] => Sony
        [3] => JVC
        [4] => Samsung
    )

)

输出

Array
(
[24] => Array
    (
        [0] => Moto E
        [1] => Moto G
    )

[22] => Array
    (
        [0] => Nokia
        [1] => Karbon
        [2] => onida
        [3] => micromax
        [4] => L'oreal
        [5] => 
    )

[21] => Array
    (
        [0] => brand1
        [1] => brand2
    )

[20] => Array
    (
        [0] => Apple
        [1] => Sony
        [2] => JVC
        [3] => Samsung
    )

)

从上面的输入数组中,如何删除重复的数组值,我的意思是' Moto G'和诺基亚'产品是重复的。因此,请提供将输入格式更改为输出格式的解决方案。

1 个答案:

答案 0 :(得分:1)

试试这个

$new_array = array();
$temp_array = array();
for($your_array as $key=>$arr_val)
{
    $arr = array();
    foreach($arr_val as $val)
    {
        if(!in_array($val, $temp_array))
        {
            $arr[] = $val;
            $temp_array[] = $val;
        }
    }

    if(sizeof($arr)>0)
    {
        $new_array[$key] = $arr;
    }
}

<强> Working Demo