按字母数字(第一个数字,然后是字母)按值排序多维数组

时间:2013-09-18 13:25:29

标签: php arrays sorting

我有这个$ 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 )....

我已经尝试了排序 asort natsort ,但到目前为止它们似乎都没有。

有什么想法吗?

4 个答案:

答案 0 :(得分:1)

foreach ($countries as $key => $row) {
    $year[$key]  = $row[0];
}


array_multisort($year, SORT_ASC, $countries);

答案 1 :(得分:1)

您必须通过 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);

答案 2 :(得分:1)

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

  

示例#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

答案 3 :(得分:0)

我已复制你的数组使其没有键值为:

$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
        )

)

希望你正在寻找这个出局。