0

我有这个 $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,但到目前为止它们似乎都不起作用。

有任何想法吗?

4

4 回答 4

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


array_multisort($year, SORT_ASC, $countries);
于 2013-09-18T13:31:26.073 回答
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);
于 2013-09-18T13:31:48.677 回答
1

尝试使用 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
于 2013-09-18T13:41:57.440 回答
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
        )

)

希望你正在寻找这个。

于 2013-09-18T13:42:24.070 回答