I asked a very similar question here and received an excellent answer.
But then I had to make one change to my array data, it broke the sort algorithm, and I don't know why.
In the previous question my array looked like this:
Array
(
[0] => Array
(
[0] => Game
[1] => Date
[2] => Site
[3] => Address
[4] => FirstName
[5] => LastName
[6] => Email
[7] => Phone
)
[1] => Array
(
[0] => B-Dry @ Blue Wave DH
[1] => 7/9/2019 13:00
[2] => Blue Wave Dover City Park
[3] => Dover
[4] => John
[5] => Doe
[6] => [email protected]
[7] => (555) 555-4797
)
[2] => Array
(
[0] => B-Dry @ Blue Wave DH
[1] => 7/9/2019 13:00
[2] => Blue Wave Dover City Park
[3] => Dover
[4] => Frank
[5] => Sinatra
[6] => [email protected]
[7] => (555) 685-5555
)
[3] => Array
(
[0] => B-Dry @ Gnaden
[1] => 6/7/2019 18:00
[2] => Gnaden Indian Valley HS
[3] => Gnadenhutten
[4] => Jimmy
[5] => Dean
[6] => [email protected]
[7] => (330) 555-5555
)
[...many more...]
)
And applying the following sort algorithm from AymDev
sorted it brilliantly:
$labelsRow = array_shift($cleanSheetArray);
$cleanSheetArray = array_map(
function ($cleanRowArray)
{
$cleanRowArray[1] = DateTime::createFromFormat('d/m/Y H:i', $cleanRowArray[1]);
return $cleanRowArray;
}
, $cleanSheetArray);
SortSheet($cleanSheetArray, 3);
function SortSheet(&$arr, $max_index = false, $index = 0) { // $index represents the number of columns to sort on, in order from the first column
function mult_usort_callback($a, $b, $max_index, $index) {
$max_index = $max_index ?: (count($a) - 1);
// Recursively sort till the max index
if ($a[$index] == $b[$index]) {
if ($index < $max_index) {
return mult_usort_callback($a, $b, $max_index, ($index + 1));
} else {
return 0;
}
}
return $a[$index] > $b[$index] ? 1 : -1;
};
usort($arr, create_function('$a, $b', 'return mult_usort_callback($a, $b, ' . $max_index . ', ' . $index . ');'));
}
This sorts my multidim array and so far everything is perfect.
But then I made a change to my array structure thusly:
[151] => Array
(
[Game] => Kaufman @ B-Dry DH
[Date] => DateTime Object
(
[date] => 2019-04-05 13:00:00.000000
[timezone_type] => 3
[timezone] => America/Denver
)
[Site] => B-Dry City Rec Park - Otsego Ave
[Address] => Coshocton
[FirstName] => Joe
[LastName] => Barnes
[Email] => [email protected]
[Phone] => (740) 000-0000
)
[152] => Array
(
[Game] => Kaufman @ B-Dry DH
[Date] => DateTime Object
(
[date] => 2019-04-05 13:00:00.000000
[timezone_type] => 3
[timezone] => America/Denver
)
[Site] => B-Dry Ridgewood High School
[Address] => Coshocton
[FirstName] =>
[LastName] =>
[Email] =>
[Phone] =>
)
[153] => Array
(
[Game] => Kaufman @ B-Dry DH
[Date] => DateTime Object
(
[date] => 2019-04-05 13:00:00.000000
[timezone_type] => 3
[timezone] => America/Denver
)
[Site] => B-Dry City Rec Park - Otsego Ave
[Address] => Coshocton
[FirstName] =>
[LastName] =>
[Email] =>
[Phone] =>
)
Observe that I changed the keys from indexers to column names. This is hugely helpful to me in processing later logic. Along with this data/key change, I attempted to change the date objectification thusly:
$cleanSheetArray = array_map(function ($cleanRowArray) {
$cleanRowArray['Date'] = DateTime::createFromFormat('d/m/Y H:i', $cleanRowArray['Date']);
return $cleanRowArray;
}, $cleanSheetArray);
That did appear to work from the standpoint of converting the string dates into date objects. But it broke the sorting. You can see evidence of the broken sort by looking at the 3 elements of the new array. Observe that array segments 151 and 153 should be consecutive because Game, Date, and Site are identical. 152 does not have identical values to 151 and 152. Game
and Date
are the same, but Site
is different. In the previous array structure (using index keys instead of named keys) the sort was correct.
I suspect that I need to make some sort of change to the SortSheet()
function, but I can't figure out what it is.
Important note: The key-name sequence will always be identical. In other words, the key sequence will always be Game, then Date, then Site, etc. So that sequence can always be counted on.
Any ideas?
See Question&Answers more detail:
os