I have looked through all the answers about sorting multidimensional arrays in PHP on Stack Overflow, but none have directly answered my question.
From the various answers I have understood that I should be using either the php usort function or the php array_multisort function, but I am not sure how to apply these to my very specific array structure:
Here is my variable $array:
Array
(
[0] => Array
(
[field1] => 10
[field2] => 100
[field3] => 100
[subarray] => Array
(
[0] => Array
(
[field1] => 10
[field2] => 100
[field3] => 100
)
[1] => Array
(
[field1] => 10
[field2] => 100
[field3] => abcORDERBYTHIS
)
)
)
[1] => Array
(
[field1] => 10
[field2] => 100
[field3] => 100
[subarray] => Array
(
[0] => Array
(
[field1] => 10
[field2] => 100
[field3] => 100
)
[1] => Array
(
[field1] => 10
[field2] => 100
[field3] => ghiORDERBYTHIS
)
)
)
[2] => Array
(
[field1] => 10
[field2] => 100
[field3] => 100
[subarray] => Array
(
[0] => Array
(
[field1] => 10
[field2] => 100
[field3] => 100
)
[1] => Array
(
[field1] => 10
[field2] => 100
[field3] => defORDERBYTHIS
)
)
)
)
I would like to be able to sort this array by field3 of the last array in subarray. Accessing this element is easy enough with the php end function like so:
<?php
foreach($array as $array_single){
foreach(end($array_single['subarray']) as $sub_array){
echo $sub_array;
}
}
?>
And from now on I'm stuck in how to specifically sort this multidimensional array alphabetically so that we get the following result:
$array[0]- Remains on top because thefield3value isabcORDERBYTHIS$array[2]- Jumps into the middle because thefield3value isdefORDERBYTHIS$array[1]- Is now at the bottom because thefield3value isghiORDERBYTHIS
Thanks in advance!