0

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 the field3 value is abcORDERBYTHIS
  • $array[2] - Jumps into the middle because the field3 value is defORDERBYTHIS
  • $array[1] - Is now at the bottom because the field3 value is ghiORDERBYTHIS

Thanks in advance!

3
  • Such advanced array sorting is often DIY. Commented Feb 14, 2013 at 17:25
  • Ouch @JasonMcCreary - not what I want to hear! ;) Commented Feb 14, 2013 at 17:28
  • Sorry. While PHP over 100 array functions, there's no silver bullet. That's not to say a combination may not provide you what you want. Commented Feb 14, 2013 at 17:43

1 Answer 1

1

Try this code:

$array = Array();

$arraytemp['field1'] = 10;
$arraytemp['field2'] = 100;
$arraytemp['field3'] = 100;
$arraytemp['subarray'][0]["field1"] = 10;
$arraytemp['subarray'][0]["field2"] = 100;
$arraytemp['subarray'][0]["field3"] = 100;
$arraytemp['subarray'][1]["field1"] = 10;
$arraytemp['subarray'][1]["field2"] = 100;
$arraytemp['subarray'][1]["field3"] = "abcORDERBYTHIS";

$array[] = $arraytemp;

$arraytemp['field1'] = 10;
$arraytemp['field2'] = 100;
$arraytemp['field3'] = 100;
$arraytemp['subarray'][0]["field1"] = 10;
$arraytemp['subarray'][0]["field2"] = 100;
$arraytemp['subarray'][0]["field3"] = 100;
$arraytemp['subarray'][1]["field1"] = 10;
$arraytemp['subarray'][1]["field2"] = 100;
$arraytemp['subarray'][1]["field3"] = "ghiORDERBYTHIS";

$array[] = $arraytemp;

$arraytemp['field1'] = 10;
$arraytemp['field2'] = 100;
$arraytemp['field3'] = 100;
$arraytemp['subarray'][0]["field1"] = 10;
$arraytemp['subarray'][0]["field2"] = 100;
$arraytemp['subarray'][0]["field3"] = 100;
$arraytemp['subarray'][1]["field1"] = 10;
$arraytemp['subarray'][1]["field2"] = 100;
$arraytemp['subarray'][1]["field3"] = "defORDERBYTHIS";

$array[] = $arraytemp;

// Sort the multidimensional array
usort($array, "custom_sort");

// Define the custom sort function used in usort
function custom_sort($a,$b) {
    return strcmp($a['subarray'][1]["field3"], $b['subarray'][1]["field3"]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Fabio! This works perfectly as expected. I am marking this as the correct answer. Thanks for the quick response !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.