Arularasan
Arularasan

Reputation: 47

Remove duplicate values from multi-dimentional array

Can anyone please explain that how to change the below input array to output array,

Input

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
    )

)

Output

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
    )

)

From the above input arrays, how to remove the duplicate array values, I mean 'Moto G' and 'Nokia' products are duplicate. So please give the solution for changing the input formats to output formats.

Upvotes: 0

Views: 82

Answers (1)

Satish Sharma
Satish Sharma

Reputation: 9635

try this

$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

Upvotes: 1

Related Questions