我如何排序这个数组的值“order”键?

尽管这些值目前是连续的,但它们并不总是连续的。

Array
(
    [0] => Array
        (
            [hashtag] => a7e87329b5eab8578f4f1098a152d6f4
            [title] => Flower
            [order] => 3
        )

    [1] => Array
        (
            [hashtag] => b24ce0cd392a5b0b8dedc66c25213594
            [title] => Free
            [order] => 2
        )

    [2] => Array
        (
            [hashtag] => e7d31fc0602fb2ede144d18cdffd816b
            [title] => Ready
            [order] => 1
        )
)

当前回答

让我们面对这个问题:PHP没有一个简单的开箱即用的函数来正确处理每个数组排序场景。

这个例程很直观,这意味着更快的调试和维护:

// Automatic population of the array
$tempArray = array();
$annotations = array();
// ... some code
// SQL $sql retrieves result array $result
// $row[0] is the ID, but is populated out of order (comes from
// multiple selects populating various dimensions for the same DATE
// for example
while($row = mysql_fetch_array($result)) {
    $needle = $row[0];
    arrayIndexes($needle);  // Create a parallel array with IDs only
    $annotations[$needle]['someDimension'] = $row[1]; // Whatever
}
asort($tempArray);
foreach ($tempArray as $arrayKey) {
    $dataInOrder = $annotations[$arrayKey]['someDimension'];
    // .... more code
}

function arrayIndexes ($needle) {
    global $tempArray;
    if (!in_array($needle, $tempArray)) {
        array_push($tempArray, $needle);
    }
}

其他回答

让我们面对这个问题:PHP没有一个简单的开箱即用的函数来正确处理每个数组排序场景。

这个例程很直观,这意味着更快的调试和维护:

// Automatic population of the array
$tempArray = array();
$annotations = array();
// ... some code
// SQL $sql retrieves result array $result
// $row[0] is the ID, but is populated out of order (comes from
// multiple selects populating various dimensions for the same DATE
// for example
while($row = mysql_fetch_array($result)) {
    $needle = $row[0];
    arrayIndexes($needle);  // Create a parallel array with IDs only
    $annotations[$needle]['someDimension'] = $row[1]; // Whatever
}
asort($tempArray);
foreach ($tempArray as $arrayKey) {
    $dataInOrder = $annotations[$arrayKey]['someDimension'];
    // .... more code
}

function arrayIndexes ($needle) {
    global $tempArray;
    if (!in_array($needle, $tempArray)) {
        array_push($tempArray, $needle);
    }
}

我使用这个函数:

function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
    $sort_col = array();
    foreach ($arr as $key => $row) {
        $sort_col[$key] = $row[$col];
    }

    array_multisort($sort_col, $dir, $arr);
}

array_sort_by_column($array, 'order');

编辑 这个答案至少有十年的历史了,现在可能有更好的解决方案,但我在一些评论中添加了一些额外的信息。

它之所以有效,是因为array_multisort()可以对多个数组进行排序。示例输入:

Array
(
    [0] => Array
        (
            [hashtag] => a7e87329b5eab8578f4f1098a152d6f4
            [title] => Flower
            [order] => 3
        )

    [1] => Array
        (
            [hashtag] => b24ce0cd392a5b0b8dedc66c25213594
            [title] => Free
            [order] => 2
        )

第一个$sort_col是一个二维数组,其中的值是我们想要排序的,键与输入数组匹配。例如,对于这个输入,选择键$sort_col "order",我们得到:

Array
(
    [0] => 3,
    [1] => 2
)

Array_multisort()然后对该数组进行排序(导致键顺序为1,0),但这只是二维数组。因此原始输入数组也作为$rest参数传递。当键匹配时,它将被排序,因此它的键也在相同的顺序中,从而给出所需的结果。

注意:

它通过引用传递,以便在适当的位置修改所提供的数组。 Array_multisort()可以像这样对多个额外的数组进行排序,而不仅仅是一个

要根据"title"键的值对数组排序,使用:

uasort($myArray, function($a, $b) {
    return strcmp($a['title'], $b['title']);
});

STRCMP比较字符串。

Uasort()维护数组键的定义。

如果有人需要根据键进行排序,最好是使用下面的方法:

usort($array, build_sorter('order'));

function build_sorter($key) {
   return function ($a, $b) use ($key) {
      return strnatcmp($a[$key], $b[$key]);
   };
}
function aasort (&$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;
}

aasort($your_array, "order");