phparraysmultidimensional-array

Arranging Array


I have a array like which is sorted by category

Array
(
[0] => Array
    (
        [name] => JOHN
        [category] => abc
    )

[1] => Array
    (
        [name] => JOHN
        [category] => abc
    )

[2] => Array
    (
    [name] => JOHN
        [category] => abc
    )

[3] => Array
    (
        [name] => John6
        [category] => cvb
    )

[4] => Array
    (
        [name] => John6
        [category] => cvb
    )

[5] => Array
    (
        [name] => Outfit7
        [category] => cvb
    )

[6] => Array
    (
        [name] => John6
        [category] => cvb
    )

[7] => Array
    (
        [name] => Joh8
        [category] => fgh
    )

[8] => Array
    (
        [name] => JOHN
        [category] => fgh
    )

[9] => Array
    (
        [name] => John9
        [category] => fgh
    )

[10] => Array
    (
        [name] => JOHN
        [category] => fgh
    )

[11] => Array
    (
        [name] => John0
        [category] => fgh
    )

[12] => Array
    (
        [name] => Johny
        [category] => fgh
    )
)

Now i want to this array deep multidimensional array from category means some thing like:

Array
{
[abc] => Array
         {
            [0] => Array
                (
                    [name] => JOHN MOFFETT
                    [category] => abc
                )
            [1] => Array
                (
                    [name] => JOHN MOFFETT
                    [category] => abc
                )
            [2] => Array
                (
                    [name] => JOHN MOFFETT
                    [category] => abc
                )
         }
[cvb] => Array
         {
            [3] => Array
                (
                    [name] => John Miller
                    [category] => cvb
                )
            [4] => Array
                (
                    [name] => John Slater
                    [category] => cvb
                )
            [5] => Array
                (
                    [name] => Outfit7
                    [category] => cvb
                )
            [6] => Array
                (
                    [name] => John Slater
                    [category] => cvb
                )
         }
}

and so on. Till now tried many ways using foreach loop and for loop but no success. Can any one give me idea on how to do this? Any help will be highly appreciated.


Solution

  • Try this:

    $tmp = ' ' ;
    
    $new_Array = array();
    
        foreach($category_array as $cat_id => $cat)
        {
           $tmp = $cat['category'];  
           $new_array[$tmp][$cat_id] = $cat;
        }