Akam Hasan Sharkar Akam Hasan Sharkar - 3 months ago 9
PHP Question

PHP group multidimensional array

I need help with grouping multidimensional PHP array. The array I have is :

Array
(
[1385] => Array
(
[product_id] => 1385
[product] => Tossed salad
[category_ids] => Array
(
[0] => 489
)
)
[1386] => Array
(
[product_id] => 1386
[product] => Green salad
[category_ids] => Array
(
[0] => 489
)
)
[1387] => Array
(
[product_id] => 1387
[product] => Milk Shake
[category_ids] => Array
(
[0] => 440
)
)
[1388] => Array
(
[product_id] => 1388
[product] => Mango Juice
[category_ids] => Array
(
[0] => 440
)
)
[1389] => Array
(
[product_id] => 1389
[product] => Orange Juice
[category_ids] => Array
(
[0] => 440
)
)
)


I want to group the array in different way so I can list them categories. Something like this :

Array
(
[category_ids] => 489,
[products] =>
[0] => Array
(
[product_id] => 1385
[product] => Tossed salad
)
[1] => Array
(
[product_id] => 1386
[product] => Green salad
)

[category_ids] => 440,
[products] =>
[0] => Array
(
[product_id] => 1387
[product] => Milk Shake
)
[1] => Array
(
[product_id] => 1388
[product] => Mango Juice
)
[2] => Array
(
[product_id] => 1389
[product] => Orange Juice
)
)


The structure can be wrong because I just made it with my text editor. But yes I want something like this. List those products under
category_ids
, sometimes there could be more then one
category_ids
as well. There many other products fields also, I shorten to make it look less complicated. There are
product_price
,
company_id
and some has multidimensional array like
product_options
.

Answer

Hi use this code to get your result

$array =  array(
    1385 => array(
            'product_id' => 1385,
            'product' => 'Tossed salad',
            'category_ids' => array(
                    0 => 489
                )
        ),
    1386 => array(
            'product_id' => 1386,
            'product' => 'Green salad',
            'category_ids' => array(
                    0 => 489
                )
        ),
    1387 => array(
            'product_id' => 1387,
            'product' => 'Milk Shake',
            'category_ids' => array(
                    0 => 440
                )
        ),
    1388 => array(
            'product_id' => 1388,
            'product' =>' Mango Juice',
            'category_ids' => array(
                    0 => 440
                )
        ),
    1389 => array(
            'product_id' => 1389,
            'product' => 'Orange Juice',
            'category_ids' => array(
                    0 => 440
                )
        )
);

$catArray = array();

foreach ($array as $key => $value) {
    foreach ($value['category_ids'] as $key2 => $value2) {
        if(isset($catArray[$value2])) {
            array_push($catArray[$value2]['products'], array('product_id' => $key,
                    'product_name' => $value['product']));
        }
        else {
            $catArray[$value2] = array(
                 'cat_id' => $value2
                );
            $catArray[$value2]['products'] =  array( 0 => 
               array(
                    'product_id' => $key,
                    'product_name' => $value['product']
                )
             );
        }
    }
}

echo '<pre/>';
print_r($catArray);