Forum Moderators: coopster

Message Too Old, No Replies

Dump Duplicate Arrays

         

Pico_Train

12:41 pm on Nov 22, 2008 (gmt 0)

10+ Year Member



Hi there! I've just tried to figure this out but I can't seem to get my head around it. In the array below, you can see that the arrays [0] and [1] are the same. I'd like to get rid of one of them if the values of var2 are equal, where $array[0]['var2'] = $array[1][var2] so I don't have a duplicate, preserving the array key does not matter. In the end I will only have a list of arrays without any dups! Thanks for the help!

$array[0] =Array
(
'var' => 'value',
'var1' => 'value1',
'var2' => 'value2',
);

$array[1] =Array
(
'var' => 'value',
'var1' => 'value1',
'var2' => 'value2',
);
$array[2] =Array
(
'var' => 'value3',
'var1' => 'value4',
'var2' => 'value5',
);

barns101

3:29 pm on Nov 22, 2008 (gmt 0)

10+ Year Member



Sorry if I'm completely misunderstanding your question, but aren't you just wanting to compare two array elements and then unset one array if the elements are the same?

if($array[0]['var2'] == $array[1]['var2'])
{
// Unset (delete) the first aray
unset($array[0]);
}

Pico_Train

5:45 am on Nov 23, 2008 (gmt 0)

10+ Year Member



Yes essentially. I thought of the above as well but didn't actually try it out. Will give it a bash now.

cameraman

5:57 am on Nov 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you looked at array_diff [us3.php.net]?