Help regrouping an array

Posted by jim on Stack Overflow See other posts from Stack Overflow or by jim
Published on 2010-12-24T09:42:53Z Indexed on 2010/12/24 9:54 UTC
Read the original article Hit count: 164

Filed under:

I'd like to regroup my array. I have the following:

Array
(
    [0] => Array
        (
            [name] => test1
            [item_id] => 1
            [category] => cat1
        )
    [1] => Array
        (
            [name] => test2
            [item_id] => 2
            [category] => cat1
        )
    [2] => Array
        (
            [name] => test3
            [item_id] => 3
            [category] => cat1
        )
    [3] => Array
        (
            [name] => test4
            [item_id] => 4
            [category] => cat2
        )
)

I'd like to regroup it like this:

Array
(
    [cat1] => Array
        (
            [0] => Array
            (
                [name] => test1
                [item_id] => 1
            )
            [1] => Array
            (
                [name] => test2
                [item_id] => 2
            )
            [2] => Array
            (
                [name] => test3
                [item_id] => 3
            )
        )
    [cat2] => Array
        (
            [name] => test4
            [item_id] => 4
        )
)

Can someone tell me how this is best done?

© Stack Overflow or respective owner

Related posts about php