0

我有一个这样的数组数组:

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

4 回答 4

4

usort 函数的经典任务:

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

于 2013-07-26T06:31:09.060 回答
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");
于 2013-07-26T06:31:29.743 回答
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
        )
)
于 2013-07-26T06:33:01.097 回答
0

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

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

[CakePHP] 制作了一个很棒的类,它能够使用点语法符号中的字符串导航到数组中。这个库被称为Hash,看看它。

方法Sort用于根据您在“路径”属性中放置的内容对具有键和值的数组进行排序。它比其他函数更有用,因为它通过保留该数组中包含的索引和所有其他子值来对数组进行排序,包括更多维的子数组。

对于您的示例:

$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,
    ),
);
于 2013-07-26T06:52:13.153 回答