<?php
$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']
];
var_export(
array_values(
array_reduce(
$array,
function ($carry, $row) {
$carry[$row['Categoria']]['Servico'] = $row['Categoria'];
$carry[$row['Categoria']]['children'][] = $row['Servico'];
return $carry;
}
)
)
);
preferences:
24.44 ms | 406 KiB | 5 Q