按值对数组排序

时间:2013-07-26 06:26:46

标签: php arrays sorting

我有一个像这样的数组:

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

问题:我如何按unique字段对其进行排序,以便将其转换为以下字段?

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

4 个答案:

答案 0 :(得分:4)

用于函数的经典任务:

<?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);

打印:

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

答案 1 :(得分:0)

使用我的自定义函数,您可以对数组进行排序

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");

答案 2 :(得分:0)

使用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);

输出:

Array
(
    [0] => Array
        (
            [unique] => 5
        )
    [1] => Array
        (
            [unique] => 3
        )
    [2] => Array
        (
            [unique] => 1
        )
)

答案 3 :(得分:0)

我觉得我在重复自己,对不起:D

不要犹豫从框架中查看库。

[CakePHP]创建了一个很棒的类,它能够使用点语法表示法中的字符串导航到数组。这个库被称为Hash,只需查看它。

方法Sort用于根据您在“path”属性中放置的内容对键和值进行排序。 它比其他函数更有用,因为它通过保持索引以及此数组中包含的所有其他子值(包括更多维度子数组)对数组进行排序。

对于你的例子:

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

请写下:

/**
 * 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');

$sorted_array将与此相同:

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