<?php
function sanitizeColumnReference($row, $columnReference) {
if (!isset($row[$columnReference]) && is_int($columnReference)) {
$columnReference = array_keys($row)[$columnReference] ?? null; // attempt to derive column name by position
if ($columnReference === null) {
throw new Exception('Failed to locate column by position using column reference: ' . $columnReference);
}
}
return $columnReference;
}
function dynamicSort(&$input, $sortingRules) {
if (!$input || !$sortingRules || !is_array($input) || !is_array($sortingRules)) {
return; // return silently
}
$firstRow = current($input);
$sortingParams = [];
foreach ($sortingRules as $rule) {
$rule = (array)$rule; // permit the passing of a solitary string as a sorting rule
$columnReference = sanitizeColumnReference($firstRow, array_shift($rule));
$column = array_column($input, $columnReference);
if (!$column) {
throw new Exception('Failed to source sortable data from column reference: ' . $columnReference);
}
$sortingParams[] = $column;
foreach ($rule as $flag) {
$sortingParams[] = constant('SORT_' . strtoupper($flag)); // convert strings to usable CONSTANTs
}
}
$sortingParams[] = &$input;
var_export($sortingParams);
array_multisort(...$sortingParams); // unpack into native sorting function
}
$array = [
['first_name' => 'Homer', 'last_name' => 'Simpson', 'city' => 'Springfield', 'state' => 'Unknown', 'zip' => '66735'],
['first_name' => 'Patty', 'last_name' => 'Bouvier', 'city' => 'Scottsdale', 'state' => 'Arizona', 'zip' => '85250'],
['first_name' => 'Moe', 'last_name' => 'Szyslak', 'city' => 'Scottsdale', 'state' => 'Arizona', 'zip' => '85255'],
['first_name' => 'Nick', 'last_name' => 'Riviera', 'city' => 'Scottsdale', 'state' => 'Arizona', 'zip' => '85255'],
];
$sortingRules = [
'city',
['zip', 'desc', 'numeric'],
[1, 'desc'], // last_name
];
echo "Sorting Params:\n";
dynamicSort($array, $sortingRules); // this modifies by reference like native sorting functions
echo "\n---\nOutput:\n";
var_export($array);
- Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- Sorting Params:
array (
0 =>
array (
0 => 'Springfield',
1 => 'Scottsdale',
2 => 'Scottsdale',
3 => 'Scottsdale',
),
1 =>
array (
0 => '66735',
1 => '85250',
2 => '85255',
3 => '85255',
),
2 => 3,
3 => 1,
4 =>
array (
0 => 'Simpson',
1 => 'Bouvier',
2 => 'Szyslak',
3 => 'Riviera',
),
5 => 3,
6 =>
array (
0 =>
array (
'first_name' => 'Homer',
'last_name' => 'Simpson',
'city' => 'Springfield',
'state' => 'Unknown',
'zip' => '66735',
),
1 =>
array (
'first_name' => 'Patty',
'last_name' => 'Bouvier',
'city' => 'Scottsdale',
'state' => 'Arizona',
'zip' => '85250',
),
2 =>
array (
'first_name' => 'Moe',
'last_name' => 'Szyslak',
'city' => 'Scottsdale',
'state' => 'Arizona',
'zip' => '85255',
),
3 =>
array (
'first_name' => 'Nick',
'last_name' => 'Riviera',
'city' => 'Scottsdale',
'state' => 'Arizona',
'zip' => '85255',
),
),
)
---
Output:
array (
0 =>
array (
'first_name' => 'Moe',
'last_name' => 'Szyslak',
'city' => 'Scottsdale',
'state' => 'Arizona',
'zip' => '85255',
),
1 =>
array (
'first_name' => 'Nick',
'last_name' => 'Riviera',
'city' => 'Scottsdale',
'state' => 'Arizona',
'zip' => '85255',
),
2 =>
array (
'first_name' => 'Patty',
'last_name' => 'Bouvier',
'city' => 'Scottsdale',
'state' => 'Arizona',
'zip' => '85250',
),
3 =>
array (
'first_name' => 'Homer',
'last_name' => 'Simpson',
'city' => 'Springfield',
'state' => 'Unknown',
'zip' => '66735',
),
)
- Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
- Parse error: syntax error, unexpected '?' in /in/JKhtR on line 5
Process exited with code 255. - Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
- Parse error: syntax error, unexpected '[' in /in/JKhtR on line 5
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
- Parse error: parse error, unexpected '[' in /in/JKhtR on line 5
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error in /in/JKhtR on line 5
Process exited with code 255.
preferences:
142.19 ms | 412 KiB | 5 Q