- I need to sort an multi dimensional array which looks like this down below (usort already applied).
- I need to move '10000_M3' to the first key if an identifier '_M' was found (as natural sorting), but the order from 'first' sort should not be touched.
- Extra: (Optional) If i have a description like '10000_0', it should be still in index 0
- If the usort can be done in 1 step would be great
-
I take any solution (usort, foreach, ..)!!!
usort($anArray, function ($a, $b) { return strnatcmp($a['description'], $b['description']); }); $anArray = [ // ? => [ // 'description' => '10000_0' // ] 0 => [ 'description' => '10000_D2' ], 1 => [ 'description' => '10000_D3' ], 2 => [ 'description' => '10000_M3' ], 3 => [ 'description' => '10000_M4' ] ]
Result (Natural -> at first position = '_0' -> $ident = '_M' after '_0' if its exists):
$result = [
0 => [
'description' => '10000_0'
]
1 => [
'description' => '10000_M3'
],
2 => [
'description' => '10000_M4'
]
3 => [
'description' => '10000_D2'
],
4 => [
'description' => '10000_D3'
],
]