...

View Full Version : Group array by key



htcilt
01-19-2010, 03:16 PM
Hi,

I have the following multidimensional array:


$testarray = array(
array(0,"group1","test1"),
array(1,"group2","test2"),
array(2,"group1","test3"),
array(3,"group3","test4"),
array(4,"group2","test5")
);

The array is dynamic so more may be added at any time i.e. the groups are not in sequence.

I need to perform a conditional statement on the array so that each item is echoed. I currently have this:


while (list($key,$val) = each($testarray)) {
echo $val[2];
}

Which outputs as:


test1
test2
test3
test4
test5

The problem is I need to keep each group together so that the output is:


test1
test3
test2
test5
test4

Can anyone help me achieve this?

PappaJohn
01-19-2010, 03:32 PM
array_multisort() (http://us3.php.net/manual/en/function.array-multisort.php)

htcilt
01-19-2010, 03:58 PM
Thanks John,

I've having trouble applying this to my example. I'm a total beginner :(



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum