To sort, you will need to group the values using the function array_map, make ordination with usort and then return the original structure with array_column.For example, you have the following original array:array (
'regs' =>
array (
'c1' =>
array (
0 => '1',
1 => '2',
2 => '3',
3 => '2',
4 => '1',
),
'c2' =>
array (
0 => '10',
1 => '20',
2 => '30',
3 => '20',
4 => '10',
),
'c3' =>
array (
0 => '100',
1 => '200',
2 => '300',
3 => '200',
4 => '400',
),
'c4' =>
array (
0 => '01:00',
1 => '02:00',
2 => '03:00',
3 => '04:00',
4 => '05:00',
),
),
)
You claim the values of c1, c2, c3 and c4 are related to each other, so we will group them into a more consistent structure with this relationship: we go through all the values related to each other in the same array using the function array_map:$agrupados = array_map(function($c1, $c2, $c3, $c4) {
return compact('c1', 'c2', 'c3', 'c4');
}, $dados['regs']['c1'], $dados['regs']['c2'], $dados['regs']['c3'], $dados['regs']['c4']);
A simplified way of doing the same would be to use the function array_values and the operator ...:$agrupados = array_map(function($c1, $c2, $c3, $c4) {
return compact('c1', 'c2', 'c3', 'c4');
}, ...array_values($dados['regs']));
The result would be:array (
0 =>
array (
'c1' => '1',
'c2' => '10',
'c3' => '100',
'c4' => '01:00',
),
1 =>
array (
'c1' => '2',
'c2' => '20',
'c3' => '200',
'c4' => '02:00',
),
2 =>
array (
'c1' => '3',
'c2' => '30',
'c3' => '300',
'c4' => '03:00',
),
3 =>
array (
'c1' => '2',
'c2' => '20',
'c3' => '200',
'c4' => '04:00',
),
4 =>
array (
'c1' => '1',
'c2' => '10',
'c3' => '400',
'c4' => '05:00',
),
)
Understand that we now have each array with the values associated with each other. Now we can order according to priority c1, c2 and c3:usort($agrupados, function ($a, $b) {
return $a['c1'] > $b['c1']
or $a['c2'] > $b['c2']
or $a['c3'] > $b['c3'];
});
This will command, but still remain the arrays with the associated values:array (
0 =>
array (
'c1' => '1',
'c2' => '10',
'c3' => '100',
'c4' => '01:00',
),
1 =>
array (
'c1' => '1',
'c2' => '10',
'c3' => '400',
'c4' => '05:00',
),
2 =>
array (
'c1' => '2',
'c2' => '20',
'c3' => '200',
'c4' => '02:00',
),
3 =>
array (
'c1' => '2',
'c2' => '20',
'c3' => '200',
'c4' => '04:00',
),
4 =>
array (
'c1' => '3',
'c2' => '30',
'c3' => '300',
'c4' => '03:00',
),
)
To return the current structure, but maintaining the ordination, we can use the function array_column, which will return one array with all the values of that given column:$ordenado = [
'c1' => array_column($agrupados, 'c1'),
'c2' => array_column($agrupados, 'c2'),
'c3' => array_column($agrupados, 'c3'),
'c4' => array_column($agrupados, 'c4')
];
In this way, the result will be:array (
'c1' =>
array (
0 => '1',
1 => '1',
2 => '2',
3 => '2',
4 => '3',
),
'c2' =>
array (
0 => '10',
1 => '10',
2 => '20',
3 => '20',
4 => '30',
),
'c3' =>
array (
0 => '100',
1 => '400',
2 => '200',
3 => '200',
4 => '300',
),
'c4' =>
array (
0 => '01:00',
1 => '05:00',
2 => '02:00',
3 => '04:00',
4 => '03:00',
),
)
What is the array in the original structure, but with the desired ordering.BonusTo group the shape you want, you can use the version $agrupados from the previous solution through all values of array and grouping those with the same values c1, c2 and c3. A simple way to generate this grouping is to generate a key in one array associative that represents these three values, so the items that have the same values will have the same keys.$resultado = [];
foreach($agrupados as $item) {
// Gera uma chave única considerando os três valores
$chave = "{$item['c1']}, {$item['c2']}, {$item['c3']}";
// Se não existir, adiciona; caso contrário agrupa os valores
if (!array_key_exists($chave, $resultado)) {
$resultado[$chave] = $item;
} else {
foreach (array_keys($item) as $k) {
if (!is_array($resultado[$chave][$k])) {
$resultado[$chave][$k] = [$resultado[$chave][$k]];
}
$resultado[$chave][$k][] = $item[$k];
}
}
}
The result will be:array (
'1, 10, 100' =>
array (
'c1' => '1',
'c2' => '10',
'c3' => '100',
'c4' => '01:00',
),
'1, 10, 400' =>
array (
'c1' => '1',
'c2' => '10',
'c3' => '400',
'c4' => '05:00',
),
'2, 20, 200' =>
array (
'c1' =>
array (
0 => '2',
1 => '2',
),
'c2' =>
array (
0 => '20',
1 => '20',
),
'c3' =>
array (
0 => '200',
1 => '200',
),
'c4' =>
array (
0 => '02:00',
1 => '04:00',
),
),
'3, 30, 300' =>
array (
'c1' => '3',
'c2' => '30',
'c3' => '300',
'c4' => '03:00',
),
)
Note that only values in r2, which have c1, c2 and c3 equal to 2, 20 and 200, respectively, which were grouped. From this simply rename the keys of the array final to r1, r2, etc as desired.