1

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)

1 Answer 1

3

Just group them first using the group value. Use that as a key in a new container assignment:

$final_arr = array();
foreach($arr as $key => $a) {
    $final_arr[$a['group']][] = $key;
}

This should yield the following structure:

Array
(
    [g1] => Array
        (
            [0] => element1
            [1] => element2
            [2] => element3
        )

    [g2] => Array
        (
            [0] => element4
            [1] => element5
            [2] => element6
        )

    [g3] => Array
        (
            [0] => element7
            [1] => element8
            [2] => element9
        )

)

Now its up to you how you'll present it (via just simple -> string or with HTML).

Dirty example:

echo '<ul>';
foreach($final_arr as $group => $f) {
    echo '<li>', $group;
    echo '<ul>'; foreach($f as $e) { echo "<li>{$e}</li>"; } echo '</ul>';
    echo '</li>';

}
echo '</ul>';

If you want that shuffle preserved, just get the keys shuffle it and transfer it:

$keys = array_keys($arr); // get the keys of the original
shuffle($keys); shuffle the keys
$new_arr = array(); // initialize a temp container
foreach($keys as $k) {
    $new_arr[$k] = $arr[$k]; // match the shuffled key and assign it
}
$arr = $new_arr; // overwrite
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It works without shuffle($arr); but it doesn't with it.
@fmalaussena shuffle eliminates the keys, that's why it doesn't work when coupled with it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.