-3

Possible Duplicate:
PHP remove duplicate values from multidimensional array

i have an array like:

Array
(
    [prom] => Array
        (
            [cab] => Array
                (
                    [0] => Array
                        (
                            [code] => 01
                            [price1] => 1000
                            [price2] => 2000
                            [available] => 2
                            [max] => 2
                            [gca] => 2
                        )

                    [1] => Array
                        (
                            [code] => 04
                            [price1] => 870
                            [price2] => 2500
                            [available] => 3
                            [max] => 4
                            [gca] => 10
                        )

                    [2] => Array
                        (
                            [code] => 01
                            [price1] => 1000
                            [price2] => 2000
                            [available] => 2
                            [max] => 2
                            [gca] => 2
                        )

                    [3] => Array
                        (
                            [code] => 05
                            [price1] => 346
                            [price2] => 1022
                            [available] => 10
                            [max] => 2
                            [gca] => 20
                        )

                )
           [cab1] => Array........

        )
    [prom1] = Array....
)

What i have to do is to remove duplicates inside every [cab*] array..

so to have something like:

Array
    (
        [prom] => Array
            (
                [cab] => Array
                    (
                        [0] => Array
                            (
                                [code] => 01
                                [price1] => 1000
                                [price2] => 2000
                                [available] => 2
                                [max] => 2
                                [gca] => 2
                            )

                        [1] => Array
                            (
                                [code] => 04
                                [price1] => 870
                                [price2] => 2500
                                [available] => 3
                                [max] => 4
                                [gca] => 10
                            )

                        [2] => Array
                            (
                                [code] => 05
                                [price1] => 346
                                [price2] => 1022
                                [available] => 10
                                [max] => 2
                                [gca] => 20
                            )

                    )
               [cab1] => Array........

            )
        [prom1] = Array....
    )

In know that there is array_unique combined with array_map to remove duplicates.. but i know that it works only on 2D array.. what can i do? can someone help me pls? thanks!!!

Community
  • 1
  • 1
Jayyrus
  • 12,961
  • 41
  • 132
  • 214
  • ok.. i read about that method super_unique that works only with 2D array.. thanks and be happy ;) – Jayyrus Sep 05 '12 at 15:47
  • is possible to delete duplicates if and only if the values inside the last array are perfectly equal? every last arrays have this key/values : code => 01,price1 => 1000,price2 => 2000,available => 2,max => 2,gca => 2. I need all this fields.. and super_unique function delete for example max=>2, max=>2.. i hope i'm clear... – Jayyrus Sep 05 '12 at 16:14

1 Answers1

1

Try it:-

function super_unique($array)
{
  $result = array_map("unserialize", array_unique(array_map("serialize", $array)));

  foreach ($result as $key => $value)
  {
    if ( is_array($value) )
    {
      $result[$key] = super_unique($value);
    }
  }

  return $result;
}


$input =  super_unique($myarray);
echo "<pre>";
print_r($input);
Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
  • is possible to delete duplicates if and only if the values inside the last array are perfectly equal? every last arrays have this key/values : code => 01,price1 => 1000,price2 => 2000,available => 2,max => 2,gca => 2. I need all this fields.. and super_unique function delete for example max=>2, max=>2.. i hope i'm clear... – Jayyrus Sep 05 '12 at 16:13
  • You want to delete max=>2, max=>2 form both of same array. am i right? – Abid Hussain Sep 05 '12 at 17:17
  • No... I don't want to delete them!!! – Jayyrus Sep 05 '12 at 19:15