【问题标题】:Sorting multidimensional PHP Array by alphabetical value [duplicate]按字母值排序多维PHP数组[重复]
【发布时间】:2014-11-17 09:26:07
【问题描述】:

我的数组如下所示:

$colors[] = array("green", "dark green");
$colors[] = array("black", "black");
$colors[] = array("green", "light green");
$colors[] = array("blue", "dark blue");
$colors[] = array("blue", "light blue");
$colors[] = array("apricote", "apricote");

我需要按子数组的第一个值的字母升序对$colors 进行排序。 (绿色、蓝色、黑色、杏色)。

我知道如何使用usort 对数字进行排序,但对字母顺序一无所知。

结果会是这样的:

$colors[] = array("apricote", "apricote");
$colors[] = array("black", "black");
$colors[] = array("blue", "dark blue");
$colors[] = array("blue", "light blue");
$colors[] = array("green", "dark green");
$colors[] = array("green", "light green");

【问题讨论】:

标签: php arrays sorting


【解决方案1】:

只使用sort()?像这样:

<?php

    $colors = array();
    $colors[] = array("green", "dark green");
    $colors[] = array("black", "black");
    $colors[] = array("green", "light green");
    $colors[] = array("blue", "dark blue");
    $colors[] = array("blue", "light blue");
    $colors[] = array("apricote", "apricote");

    sort($colors);

    print_r($colors);

?>

输出:

    Array
(
    [0] => Array
        (
            [0] => apricote
            [1] => apricote
        )

    [1] => Array
        (
            [0] => black
            [1] => black
        )

    [2] => Array
        (
            [0] => blue
            [1] => dark blue
        )

    [3] => Array
        (
            [0] => blue
            [1] => light blue
        )

    [4] => Array
        (
            [0] => green
            [1] => dark green
        )

    [5] => Array
        (
            [0] => green
            [1] => light green
        )

)

【讨论】:

  • 天哪,我糟透了。谢谢
  • @KristianRafteseth 欢迎您!祝你有美好的一天:D
猜你喜欢
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
相关资源
最近更新 更多