0

私はStackOverflowでPHPの多次元配列をソートすることについてのすべての答えを調べましたが、私の質問に直接答えたものはありません。

さまざまな回答から、phpusort関数またはphp関数のいずれかを使用する必要があることを理解しarray_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この配列をの最後の配列でソートできるようにしたいと思いますsubarrayendこの要素へのアクセスは、次のようなphp関数を使用すると簡単です。

<?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 に答える