0

I have this $countries array:

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

I would like is to sort it by ascending order of the year so I'd have something like

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

I've tried sort, asort and natsort but none of them seem to work so far.

Any ideas?

IreM
  • 1
  • 4

4 Answers4

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


array_multisort($year, SORT_ASC, $countries);
Ruben Serrate
  • 2,724
  • 1
  • 17
  • 21
1

You have to sort multidimensional arrays by array_multisort function.

First you have to prepare sorting array then you apply sorting itself:

$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);
muffir
  • 447
  • 5
  • 16
  • Tried this, still getting them in the original order – IreM Sep 18 '13 at 13:40
  • What is your PHP version? It works for me in 5.3.21 but I'm pretty sure it works in a few minor versions back. – muffir Sep 18 '13 at 15:18
  • Using 5.3.23, I really can't understand how the ordering is working. I'm trying different constants in the 2nd part line DESC, NUMERIC etc and it's slightly changing however I never get to see 2012 on the first line. – IreM Sep 18 '13 at 15:36
  • The principle is as follows: the function sorts the first array (set of year values) in order defined by sorting type in second parameter (there are couple of constants for that, see [documentation](http://php.net/manual/en/function.array-multisort.php)). Meanwhile it sorts the array in the third argument in the **same order** as the first array is being sorted. As you can see _Ruben Serrate Pardo_ posted quite the same solution so this should work. Unfortunately I have no idea why this is not working for you. – muffir Sep 19 '13 at 08:04
1

Try using usort. Look at example #2 in the documentation (http://www.php.net/manual/en/function.usort.php ):

Example #2 usort() example using multi-dimensional array

<?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";
}
?>

When sorting a multi-dimensional array, $a and $b contain references to the first index of the array. The above example will output:

$fruits[0]: apples
$fruits[1]: grapes
$fruits[2]: lemons
lucas
  • 1,050
  • 12
  • 21
  • I've tried using usort, I'm a php newbie, I got a bunch of warnings back, so I really don't think I used it correctly. – IreM Sep 18 '13 at 14:19
0

I have copied your array make it without key value as:

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

After that I have use :

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

And out put was:

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
        )

)

Hope you are looking for this out put.

Rajiv Ranjan
  • 1,869
  • 1
  • 11
  • 20