0

我在 Stack Overflow 上浏览了所有关于在 PHP 中对多维数组进行排序的答案,但没有一个直接回答我的问题。

从各种答案中,我了解到我应该使用 phpusort函数或 phparray_multisort函数,但我不确定如何将它们应用于我非常具体的数组结构:

这是我的变量$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
                        )

                )

        )

)

希望能够field3按. 使用 php函数访问这个元素很容易,如下所示:subarrayend

<?php
foreach($array as $array_single){
    foreach(end($array_single['subarray']) as $sub_array){
        echo $sub_array;
    }
}
?>

从现在开始,我被困在如何按字母顺序专门对这个多维数组进行排序,以便我们得到以下结果:

  • $array[0]- 保持领先,因为field3价值是abcORDERBYTHIS
  • $array[2]- 跳到中间,因为field3值是defORDERBYTHIS
  • $array[1]- 现在位于底部,因为field3值为ghiORDERBYTHIS

提前致谢!

4

1 回答 1

1

试试这个代码:

$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"]);
}
于 2013-02-14T18:16:31.193 回答