How to split a selectbox in a form for the same entity

I have a shoppingcart table, a products table and a productcategory table. A shoppingcart can have products and all products have a category. When I go to the edit view of the shoppingcart, I can see all available products in my selectbox and the products that are allready in the shoppingcart are selected.

This is the code for the selectbox I have:

$this->Form->control(‘products._ids’, [
‘type’ => ‘select’,
‘multiple’ => ‘checkbox’,
‘options’ => $products,
‘default’ => array_keys($products)

But I would like to split the selectbox by the product category.

See the following picture for better explanation:

How can I do this and how would I recombine them again in the controller?

They need to be 2 seperate selectboxes, not only a single one that is sorted by the category.

Thanks

You need to make your $products a two-dimensional array instead of one. You currently have something like:

[1 => 'Pan', 2 => 'Shirt', 3 => 'Spoon', 4 => 'Hat']

You need

['Kitchen' => [1 => 'Pan', 3 => 'Spoon'], 'Clothes' => [2 => 'Shirt', 4 => 'Hat']]

You can get to something like that with, for example, the groupBy function of collections.