【问题标题】:Sorting multidimensional array by values alphanumerically (first numbers, then letters) [duplicate]按字母数字顺序对多维数组进行排序(第一个数字,然后是字母)[重复]
【发布时间】:2013-09-23 06:53:22
【问题描述】:

我有这个 $countries 数组:

Array ( [0] => 2013 Germany [1] => country [2] => Berlin [3] => Beer)
Array ( [0] => 2012 Italy [1] => country  [2] => Rome [3] => Wine  )
Array ( [0] => 2013 Germany [1] => country  [2] => Munich [3] => Beer )
Array ( [0] => 2013 Germany [1] => country  [2] => Dusseldorf [3] => Beer )
Array ( [0] => 2013 Italy [1] => country  [2] => Venice [3] => Wine )
Array ( [0] => 2013 Russia ....) etc

我想按年份的升序对其进行排序,所以我会有类似

Array ( [0] => 2012 Italy [1] => country  [2] => Rome [3] => Wine  )
Array ( [0] => 2013 Germany [1] => country [2] => Berlin [3] => Beer)
Array ( [0] => 2013 Germany [1] => country  [2] => Munich [3] => Beer )....

我尝试过 sortasortnatsort,但到目前为止,它们似乎都不起作用。

有什么想法吗?

【问题讨论】:

  • 所以你错过了神奇的usort()
  • asort 应该可以工作,我累了,工作正常。

标签: php arrays sorting


【解决方案1】:
foreach ($countries as $key => $row) {
    $year[$key]  = $row[0];
}


array_multisort($year, SORT_ASC, $countries);

【讨论】:

    【解决方案2】:

    您必须通过 array_multisort 函数对多维数组进行排序。

    首先你必须准备排序数组,然后你自己应用排序:

    $sortArr = array();
    
    // prepare the sorting array
    foreach ($arrayToSort as $row) {
      $sortArr[] = $row[0];  // put there the year value
    }
    
    // sorting - first param is helper array, then constant with sorting direction, third param is array you wish to sort
    array_multisort($sortArr, SORT_ASC, $arrayToSort);
    

    【讨论】:

    • 试过了,还是按照原来的顺序来的
    • 你的 PHP 版本是多少?它在 5.3.21 中适用于我,但我很确定它在几个小版本中也适用。
    • 使用 5.3.23,我真的无法理解排序是如何工作的。我在第 2 部分行 DESC、NUMERIC 等中尝试不同的常量,但它略有变化,但我从未在第一行看到 2012。
    • 原理如下:函数按照第二个参数中排序类型定义的顺序对第一个数组(年份值的集合)进行排序(有几个常量,参见documentation)。同时,它以 与第一个数组相同的顺序 对第三个参数中的数组进行排序。如您所见,Ruben Serrate Pardo 发布了完全相同的解决方案,因此这应该可以工作。不幸的是,我不知道为什么这对你不起作用。
    【解决方案3】:

    尝试使用 usort。查看文档中的示例 #2 (http://www.php.net/manual/en/function.usort.php ):

    Example #2 使用多维数组的 usort() 示例

    <?php
    function cmp($a, $b)
    {
        return strcmp($a["fruit"], $b["fruit"]);
    }
    
    $fruits[0]["fruit"] = "lemons";
    $fruits[1]["fruit"] = "apples";
    $fruits[2]["fruit"] = "grapes";
    
    usort($fruits, "cmp");
    
    while (list($key, $value) = each($fruits)) {
        echo "\$fruits[$key]: " . $value["fruit"] . "\n";
    }
    ?>
    

    对多维数组进行排序时,$a 和 $b 包含引用 到数组的第一个索引。上面的例子会输出:

    $fruits[0]: apples
    $fruits[1]: grapes
    $fruits[2]: lemons
    

    【讨论】:

    • 我试过用usort,我是php新手,收到了一堆警告,所以我真的不认为我用对了。
    【解决方案4】:

    我已经复制了你的数组,使它没有键值:

    $test = array (Array (  '2013 Germany',  'country',  'Berlin ', 'Beer'),
    Array (  '2012 Italy' , 'country'  , 'Rome',  'Wine'  ),
    Array (  '2013 Germany' , 'country' ,  'Munich' ,  'Beer' ),
    Array (  '2013 Germany',  'country' ,  'Dusseldorf',  'Beer' ),
    Array (  '2013 Italy' , 'country' ,  'Venice' , 'Wine' )
    );
    

    在那之后我已经使用了:

    asort($test);
    $test = array_values($test);    
    print_r($test);
    

    结果是:

    Array
    (
        [0] => Array
            (
                [0] => 2012 Italy
                [1] => country
                [2] => Rome
                [3] => Wine
            )
    
        [1] => Array
            (
                [0] => 2013 Germany
                [1] => country
                [2] => Berlin 
                [3] => Beer
            )
    
        [2] => Array
            (
                [0] => 2013 Germany
                [1] => country
                [2] => Dusseldorf
                [3] => Beer
            )
    
        [3] => Array
            (
                [0] => 2013 Germany
                [1] => country
                [2] => Munich
                [3] => Beer
            )
    
        [4] => Array
            (
                [0] => 2013 Italy
                [1] => country
                [2] => Venice
                [3] => Wine
            )
    
    )
    

    希望您正在寻找这个输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      相关资源
      最近更新 更多