So I have an array like so :
$arr = array(
"element1" => array("group" => "g1", "other_stuff" => "foo"),
"element2" => array("group" => "g1", "other_stuff" => "bar"),
"element3" => array("group" => "g1", "other_stuff" => "foo"),
"element4" => array("group" => "g2", "other_stuff" => "bar"),
"element5" => array("group" => "g2", "other_stuff" => "foo"),
"element6" => array("group" => "g2", "other_stuff" => "bar"),
"element7" => array("group" => "g3", "other_stuff" => "foo"),
"element8" => array("group" => "g3", "other_stuff" => "bar"),
"element9" => array("group" => "g3", "other_stuff" => "foo"),
);
I'd like to display the elements in a shuffled order within each group, preceded by the group, the order of the groups being shuffled too. So for instance :
g2
-> element5
-> element4
-> element6
g3
-> element7
-> element9
-> element8
g1
-> element2
-> element3
-> element1
I thought I could do something along this line :
shuffle($arr);
foreach($arr[]["group"] as $current_group){
echo "$current_group"
foreach($arr[][$current_group] as $current_element){
echo "-> ".$ current_element."<br />"
}
}
But no, $arr[]["group"] isn't valid. How can I achieve that ?
Thanks
(It's 2AM where I am so my pseudo code might be uber broken, sorry, I just want to convey the idea I was following)