1

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
            )
        )
)

2 Answers 2

5

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

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
@i-like-php i forgot the square brackets in the last line. it should work now
need to unset cat_id also. change above unset($item['cat_name'], $item['cat_id'])';
4
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;
}

2 Comments

Your answer is also superb! don't b sad . better luck next time.i"ll choose your answer
No worries, I guess we all do this just for the fun of it. Great if it happens to help sometimes :)

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.