0

I've got an array of arrays like this :

Array(
    [id1] => Array (
           /* details */
           [unique] => 3
          )
    [id2] => Array (
           /* details */
           [unique] => 5
    [id3] => Array (
           /* details */
           [unique] => 1
    )
)

Question : How could I sort it by the unique field, so that it is converted like the one following?

Array(
    [id2] => Array (
           /* details */
           [unique] => 5
          )
    [id1] => Array (
           /* details */
           [unique] => 3
    [id3] => Array (
           /* details */
           [unique] => 1
    )
)
Ryan
  • 26,884
  • 9
  • 56
  • 83
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223

4 Answers4

4

Classical task for usort function:

<?php
$data = array(
    "id1" => Array (
        "unique" => 3
        ),
    "id2" => Array (
        "unique" => 5),
    "id3" => Array (
        "unique" => 1
        ),
    );

function cmp($a, $b)
// {{{
{
    if ($a["unique"] == $b["unique"]) {
        return 0;
    }
    return ($a["unique"] < $b["unique"]) ? 1 : -1;
}
// }}}

usort($data, "cmp");
var_dump($data);

Prints:

array(3) {
  [0]=>
  array(1) {
    ["unique"]=>
    int(5)
  }
  [1]=>
  array(1) {
    ["unique"]=>
    int(3)
  }
  [2]=>
  array(1) {
    ["unique"]=>
    int(1)
  }
}

http://www.php.net/manual/en/function.usort.php

user4035
  • 22,508
  • 11
  • 59
  • 94
0

using my custom function you can sort array

function multisort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

multisort ($your_array,"order");
liyakat
  • 11,825
  • 2
  • 40
  • 46
0

By using usort():

$array = array(
    "id1" => array("unique" => 3),
    "id2" => array("unique" => 5),
    "id3" => array("unique" => 1),
);

usort($array, function ($a, $b){
    if ($a['unique'] == $b) {
        return 0;
    }
    return ($a['unique'] > $b['unique']) ? -1 : 1;
});

print_r($array);

Outputs:

Array
(
    [0] => Array
        (
            [unique] => 5
        )
    [1] => Array
        (
            [unique] => 3
        )
    [2] => Array
        (
            [unique] => 1
        )
)
Hein Andre Grønnestad
  • 6,885
  • 2
  • 31
  • 43
0

I feel like I'm repeating myself, sorry :D

Do not hesitate to look at libraries from frameworks.

[CakePHP] made an awesome class which is able to navigate into arrays using a string in dot syntax notation. This library is known as Hash, just look at it.

The method Sort is used to sort an array with keys and values depending of what you put inside the "path" attribute. It's more useful than others functions, because it sorts the array by keeping the indexes and all the other subvalues contained in this array, including more dimensional sub-arrays.

For your example :

$array = array(
    'id1' => array (
        'unique' => 3,
    ),
    'id2' => array (
        'unique' => 5,
    ),
    'id3' => Array (
        'unique' => 1,
    ),
);

Just write this :

/**
 * First param: the array
 * Second param : the "path" of the datas to save.
 *  "{s}" means "Matching any kind of index", for you to sort ALL indexes matching
 *  the dot "." is used to navigate into the array like an object
 *  And "unique" is the key which you want to be sorted by its value
 * The third param is simply the order method, "ascending" or "descending"
*/
$sorted_array = Hash::sort($array, '{s}.unique', 'asc');

And $sorted_array will be equal to this :

array (
    'id3' => array (
        'unique' => 1,
    ),
    'id1' => array (
        'unique' => 3,
    ),
    'id2' => array (
        'unique' => 5,
    ),
);
Alex Rock
  • 831
  • 6
  • 26