Have an account? Sign in
Login  Register  Facebook
array_unique problem
hi i have problem with array_unique

i want to change this array :

Array
(
    [1] => Array
        (
            [id] => 12
            [name] => Great man
        )

    [2] => Array
        (
            [id] => 12
            [name] => Great man
        )

    [3] => Array
        (
            [id] => 12
            [name] => Great man
        )

    [4] => Array
        (
            [id] => 14
            [name] => Lamborgini
        )

    [5] => Array
        (
            [id] => 14
            [name] => Lamborgini
        )

    [6] => Array
        (
            [id] => 14
            [name] => Lamborgini
        )

    [7] => Array
        (
            [id] => 14
            [name] => Lamborgini
        )

    [8] => Array
        (
            [id] => 29
            [name] => Leon
        )

    [9] => Array
        (
            [id] => 29
            [name] => Leon
        )

    [10] => Array
        (
            [id] => 29
            [name] => Leon
        )

    [11] => Array
        (
            [id] => 29
            [name] => Leon
        )

    [12] => Array
        (
            [id] => 29
            [name] => Leon
        )

)
to :
Array
(
    [1] => Array
        (
            [id] => 12
            [name] => Great man
        )

    [2] => Array
        (
            [id] => 14
            [name] => Lamborgini
        )

    [3] => Array
        (
            [id] => 29
            [name] => Leon
        )

)


but with array_unique cant do that


do you have a function to unique first array?
Started: September 21, 2011 Latest Activity: September 21, 2011 array array_unique
2 Answers
see this function:
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;
}
use it like:
    $array = array(
        1 => array('id' => '12','name' => 'Great man'),
        1 => array('id' => '12','name' => 'Great man'),
        3 => array('id' => '12','name' => 'Great man'),
        4 => array('id' => '14','name' => 'Lamborgini'),
        5 => array('id' => '14','name' => 'Lamborgini'),
        6 => array('id' => '14','name' => 'Lamborgini'),
        7 => array('id' => '14','name' => 'Lamborgini'),
        8 => array('id' => '29','name' => 'Leon'),
        9 => array('id' => '29','name' => 'Leon'),
        10 => array('id' => '29','name' => 'Leon'),
        11 => array('id' => '29','name' => 'Leon'),
        11 => array('id' => '29','name' => 'Leon'), 
    );
 
    print_r(super_unique($array)); 

Posted: MacOS
In: September 21, 2011

thanks very much
September 21, 2011

sorry i have another question how can i return an array like : [php] $array = array( 1 => array('id' => '12','name' => 'Great man'), 2 => array('id' => '14','name' => 'Lamborgini'), 3 => array('id' => '29','name' => 'Leon'), ); [/php] [color=red]please attention to first keys[/color]
September 21, 2011

You got it right, what is the problem?
September 21, 2011

you can use $array like any PHP var. e.g.:[php]foreach($array as $value) { // now use $value['id'] or $value['name']; // sample: echo 'ID: '.$value['id'].'<br/>'; echo 'NAME: '.$value['name'].'<br/>'; } [/php]and[php]$great_man = $array[1]; echo 'id: '.$great_man['id'].' name: '.$great_man['name']; [/php]
September 21, 2011

is this not working?
	$array = array(
        1 => array('id' => '12','name' => 'Great man'),
        1 => array('id' => '12','name' => 'Great man'),
        3 => array('id' => '12','name' => 'Great man'),
        4 => array('id' => '14','name' => 'Lamborgini'),
        5 => array('id' => '14','name' => 'Lamborgini'),
        6 => array('id' => '14','name' => 'Lamborgini'),
        7 => array('id' => '14','name' => 'Lamborgini'),
        8 => array('id' => '29','name' => 'Leon'),
        9 => array('id' => '29','name' => 'Leon'),
        10 => array('id' => '29','name' => 'Leon'),
        11 => array('id' => '29','name' => 'Leon'),
        11 => array('id' => '29','name' => 'Leon'), 
    );
    
    //one
    print_r($array);
    //two
    print_r(array_unique($array));

Posted: Go
In: September 21, 2011

no array_unique is not working for multi dimensional array
September 21, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer