I have an array like this one:
$array = [
['Categoria' => 'example', 'Servico' => 'name1'],
['Categoria' => 'example', 'Servico' => 'name2'],
['Categoria' => 'example', 'Servico' => 'name3'],
['Categoria' => 'example2', 'Servico' => 'name4'],
['Categoria' => 'example2', 'Servico' => 'name5'],
['Categoria' => 'example2', 'Servico' => 'name6'],
['Categoria' => 'example3', 'Servico' => 'name7'],
['Categoria' => 'example3', 'Servico' => 'name8'],
['Categoria' => 'example3', 'Servico' => 'name9']
];
I need to transform this on something like:
[
[
'Servico' => 'example',
'children' => [
['Servico' => 'name1'],
['Servico' => 'name2'],
['Servico' => 'name3'],
]
],
[
'Servico' => 'example2',
'children' => [
['Servico' => 'name4'],
['Servico' => 'name5'],
['Servico' => 'name6'],
]
],
[
'Servico' => 'example3',
'children' => [
['Servico' => 'name7'],
['Servico' => 'name8'],
['Servico' => 'name9'],
]
],
]
I have read this topic and I successfully have grouped my array, but I couldn't find a way to format the array in a way I have Servico
and children
on each object.
Someone have any ideas?