0

My array looks like this:

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

I need to sort $colors alphabetically ascending by the first value of the subarrays. (green, blue, black, apricote).

I know how to use usort for sorting numerical, but dont have any clue about alphabetical.

The result would be something like this:

$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");
Kristian Rafteseth
  • 2,002
  • 5
  • 27
  • 46

1 Answers1

1

Just use sort()? Like this:

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

?>

Output:

    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
        )

)
Rizier123
  • 58,877
  • 16
  • 101
  • 156