簡體   English   中英

如何在PHP 5.4中對多維數組進行排序?

[英]How to sort multidimensional array in PHP version 5.4?

我有一個數組結構,其print_r輸出如下所示:

Array
(
    [0] => Array
        (
            [0] => Game
            [1] => Date
            [2] => Site
            [3] => Address
            [4] => FirstName
            [5] => LastName
            [6] => Email
            [7] => Phone
        )

    [1] => Array
        (
            [0] => B-Dry @ Blue Wave DH
            [1] => 7/9/2019 13:00
            [2] => Blue Wave Dover City Park
            [3] => Dover
            [4] => John
            [5] => Doe
            [6] => john.doe@perrylocal.org
            [7] => (555) 555-4797
        )

    [2] => Array
        (
            [0] => B-Dry @ Blue Wave DH
            [1] => 7/9/2019 13:00
            [2] => Blue Wave Dover City Park
            [3] => Dover
            [4] => Frank
            [5] => Sinatra
            [6] => frank@sinatra.com
            [7] => (555) 685-5555
        )

    [3] => Array
        (
            [0] => B-Dry @ Gnaden
            [1] => 6/7/2019 18:00
            [2] => Gnaden Indian Valley HS
            [3] => Gnadenhutten
            [4] => Jimmy
            [5] => Dean
            [6] => jimmy@dean.org
            [7] => (330) 555-5555
        )
   [...many more...]
)

數據來自excel電子表格。 如果它來自SQL,排序將是微不足道的。 :-)

我如何排序/重新排序此數組,如下所示:

  1. 忽略第一個數組集(列標簽,而不是數據)
  2. 首先按索引0排序(例如:“ B-Dry @ Blue Wave DH”)
  3. 按索引1排序第二位(例如:“ 7/9/2019 13:00”)
  4. 按索引2排名第三(例如:“藍色波浪多佛城市公園”)

這篇文章看起來很有希望,但是我相信array_column僅在PHP 5.5中有效,而我還不能升級到PHP 5.5。 而且我不確定在所有情況下如何排除第一個數組集(列標簽)。

這是一個函數,它將根據所需的列數對多維數組進行排序:

function mult_usort(&$arr, $max_index = false, $index = 0) {

    // Done on purpose, could not use a closure
    function mult_usort_callback($a, $b, $max_index, $index) {
        $max_index = $max_index ?: (count($a) - 1);

        // Recursive to sort till the max index
        if ($a[$index] == $b[$index]) {
            if ($index < $max_index) {
                return mult_usort_callback($a, $b, $max_index, ($index + 1));
            } else {
                return 0;
            }
        }
        return $a[$index] > $b[$index] ? 1 : -1;
    };

    usort($arr, create_function('$a, $b', 'return mult_usort_callback($a, $b, ' . $max_index . ', ' . $index . ');'));
}

並像這樣使用它:

// Remove the 1st item
array_shift($data);

// Change the date & time column to DateTime objects for proper comparison
// DateTime: PHP >= 5.2
$data = array_map(function ($item) {
    $item[1] = DateTime::createFromFormat('d/m/Y H:i', $item[1]);
    return $item;
}, $data);

// Then sort with the function, till index 2
mult_usort($data, 2);

該功能使用:


如果您的數據是SQL查詢的結果,則最好在查詢中進行ORDER ,這將更加輕松快捷
另請參見: 關聯數組的更新函數

要擺脫第一個元素,只需:

unset($array[0]);

然后將usort函數與DateTime比較一起使用:

    <?php

$data = [
    [
            'Game',
            'Date',
            'Site',
            'Address',
            'FirstName',
            'LastName',
            'Email',
            'Phone'
    ],
    [
            'B-Dry @ Gnaden',
            '6/7/2019 18:00',
            'Gnaden Indian Valley HS',
            'Gnadenhutten',
            'Jimmy',
            'Dean',
            'jimmy@dean.org',
            '(330) 555-5555'
    ],
    [
            'B-Dry @ Blue Wave DH',
            '7/9/2019 19:00',
            'Blue Wave Dover City Park',
            'Dover',
            'John',
            'Doe',
            'john.doe@perrylocal.org',
            '(555) 555-4797'
    ],
    [
            'B-Dry @ Blue Wave DH',
            '7/9/2019 13:00',
            'Blue Wave Dover City Park',
            'Dover',
            'Frank',
            'Sinatra',
            'frank@sinatra.com',
            '(555) 685-5555'
    ]
];

    unset($data[0]);

    usort($data, function ($a, $b) {
       if ($a[0] == $b[0]) {
           $d1 = DateTime::createFromFormat('d/m/y h:i',$a[1]);
           $d2 = DateTime::createFromFormat('d/m/y h:i',$b[1]);
           if ($d1 == $d2) {
                if ($a[2] == $b[2]) {
                    return 0;
                }
                return ($a[2] < $b[2]) ? -1 : 1;
           }
           return ($d1 < $d2) ? -1 : 1;
       }
       return ($a[0] < $b[0]) ? -1 : 1;
    });

    var_dump($data, true);

在以下位置檢查: https//3v4l.org/uMacS

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM