0

i have a 2-dimensionl array such as this:

  Array
(
    [0] => Array
    (
        [0] => talk
        [1] => amount
    )
    [1] => Array
    (
        [0] => base
        [1] => amazing
    )
    [2] => Array
    (
        [0] => talk
        [1] => filter
    )
    [3] => Array
    (
        [0] => label
        [1] => any
    )
    [4] => Array
    (
        [0] => talk
        [1] => amount
    )
    [5] => Array
    (
        [0] => tour
        [1] => any
    )
)

how remove duplicate value by first dimension and result such as this:

  Array
(
    [0] => Array
    (
        [0] => talk
        [1] => amount
    )
    [1] => Array
    (
        [0] => base
        [1] => amazing
    )
    [2] => Array
    (
        [0] => label
        [1] => any
    )
    [3] => Array
    (
        [0] => tour
        [1] => any
    )
)

there is much help about remove duplicate values in the array, but I didn't see any sample about remove duplicates by specific dimension.

Wenfang Du
  • 8,804
  • 9
  • 59
  • 90

1 Answers1

0
<?php
function uniqueArray($array)
{
  $result = array_map("unserialize", array_unique(array_map("serialize", $array)));

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

  return $result;
}
?>

This will helps you

Note: not tested