0

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.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
Arularasan
  • 47
  • 10
  • 1
    Have you actually tried anything before asking others to write the code for you? – Aleks G Jul 21 '14 at 11:05
  • possible duplicate of [How to remove duplicate values from a multi-dimensional array in PHP](http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – Manibharathi Jul 21 '14 at 11:06
  • this isnt a 'write the code for you' site - it is here to help you when you have tried something and got stuck! – GrahamTheDev Jul 21 '14 at 11:06
  • I voted duplicate, but wanted to indicate: I understand you can't find that solution. Don't see the duplicate as a punishment :) – Martijn Jul 21 '14 at 11:07
  • it is not duplicate.in this case the inner arrays contain different values so those solutions will not work for this. – Sougata Bose Jul 21 '14 at 11:49

1 Answers1

1

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

Satish Sharma
  • 9,547
  • 6
  • 29
  • 51