Forum Moderators: coopster

Message Too Old, No Replies

Can you use array_unique on a multidimensional array?

Or not?

         

HughMungus

5:00 am on Apr 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have an array that consists of other arrays where the first value in all the arrays is the same (say, the first value in each array is "widgets"). Every time I try to do array_unique on it, it returns only one value (widgets something something) instead of the many it should be returning. I think what's happening is that array_unique is looking at only the first value of each array so I get only one result (since all the arrays have "widgets" as its first value).

I've been doing some research and it looks like others have had the same problem.

Given a multidimensional array, is there a way to do an array_unique type function? An example with a foreach or while loop would be great. TIA.

coopster

1:08 pm on May 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm not sure I quite understand what you are looking for yet so you may have to give some example arrays for clarification. This is one way to get duplicate values cleaned up...
$input = array( 
"veggies1" => array(
0 => "parsnips",
1 => "carrots",
2 => "tomatoes"),
"veggies2" => array(
3 => "potatoes",
4 => "potatoes", // gets rid of this one!
5 => "peas",
6 => "carrots")
);
foreach ($array as $key => $value) $array[$key] = array_unique($value);