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?
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 2 of 2 people found this answer helpful. Did you? Yes No thanks very much 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] You got it right, what is the problem? 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]
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 0 of 0 people found this answer helpful. Did you? Yes No no array_unique is not working for multi dimensional array |
© Advanced Web Core. All rights reserved