views:

64

answers:

2

i have an array like this

Array
(
    [0] => Array
        (
            [cat_name] => Clothing
            [cat_id] => 1
            [item_name] => shirt
            [item_id] => 1
            [src] => 177
            [sic] => 78
        )

    [1] => Array
        (
            [cat_name] => Stationary
            [cat_id] => 3
            [item_name] => note book
            [item_id] => 8
            [src] => 50
            [sic] => 10
        )

    [2] => Array
        (
            [cat_name] => Stationary
            [cat_id] => 3
            [item_name] => ball pen
            [item_id] => 10
            [src] => 59
            [sic] => 58
        )

    [3] => Array
        (
            [cat_name] => Expandable
            [cat_id] => 4
            [item_name] => vim powder
            [item_id] => 14
            [src] => 34
            [sic] => 23
        )

    [4] => Array
        (
            [cat_name] => Clothing
            [cat_id] => 1
            [item_name] => pant
            [item_id] => 16
            [src] => 100
            [sic] => 10
        )

)

now what i want first it sorted by cat_id and then a create a new array having below structure

Array
(
    [0] =>"Clothing"=>Array
        (

          [0]=>Array
            (
                [item_name] => shirt
                [item_id] => 1
                [src] => 177
                [sic] => 78
            )
          [1] => Array
            (
                [item_name] => pant
                [item_id] => 16
                [src] => 100
                [sic] => 10
            )

        )
    [1] => "Stationary"=>Array
        (
         [0] => Array
            (   
                [item_name] => note book
                [item_id] => 8
                [src] => 50
                [sic] => 10
            )

        [1] => Array
            (
                [item_name] => ball pen
                [item_id] => 10
                [src] => 59
                [sic] => 58
            )

        )
    [2]=>"Expandable => Array
        (
        [0] => Array
            (
                [item_name] => vim powder
                [item_id] => 14
                [src] => 34
                [sic] => 23
            )
        )
)
+1  A: 

Untested

$output = array();
foreach($array as $item) {
    if(!isset($output[$item['cat_name']])) {
        $output[$item['cat_name']] = array();
    }
    $catName = $item['cat_name'];
    unset($item['cat_name']);
    $output[$catName][] = $item;
}
unset
Thanks for quick reply. it works! but it outputs only single data of each category.(i.e latest item of each category)i want multidimensional array of each category in which all item details shoule be listed
diEcho
@i-like-php i forgot the square brackets in the last line. it should work now
unset
Yes! now this works! both answers are good... Thanks Buddy
diEcho
need to unset cat_id also.change above `unset($item['cat_name'], $item['cat_id'])'`;
diEcho
+2  A: 
function cmp($a, $b)
{
    if ($a['cat_id'] == $b['cat_id']) {
        return 0;
    }
    return ($a['cat_id'] < $b['cat_id']) ? -1 : 1;
}
// sort by cat_id 
usort($array, 'cmp');

// create the grouped array
$res = array();
foreach($array as &$item) {
    $cat_name = $item['cat_name'];
    unset($item['cat_name'], $item['cat_id']);
    $res[$cat_name][] = $item;
}
nuqqsa
Great work! this is waht i want exactly. Thaksssss
diEcho
Your answer is also superb! don't b sad . better luck next time.i"ll choose your answer
diEcho
No worries, I guess we all do this just for the fun of it. Great if it happens to help sometimes :)
nuqqsa