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.

5
  • 1
    Have you actually tried anything before asking others to write the code for you? Commented Jul 21, 2014 at 11:05
  • possible duplicate of How to remove duplicate values from a multi-dimensional array in PHP Commented Jul 21, 2014 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! Commented Jul 21, 2014 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 :) Commented Jul 21, 2014 at 11:07
  • it is not duplicate.in this case the inner arrays contain different values so those solutions will not work for this. Commented Jul 21, 2014 at 11:49

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your code.. It will help me to assign the supplier for the particular brand in my applications

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.