1

I have multi-dimension array like:

Array
(
    [name] => Array
        (
            [0] => South Africa
            [1] => Australia
            [2] => Egypt
        )

    [img] => Array
        (
            [0] => sa-flag.jpg
            [2] => au-flag.jpg
            [1] => eg-flag.jpg
        )
)

and I want to sort it alphabetically such that its output will exactly like:

Array
(
    [name] => Array
        (
            [0] => Australia
            [1] => Egypt
            [2] => South Africa
        )

    [img] => Array
        (
            [0] => au-flag.jpg
            [2] => eg-flag.jpg
            [1] => sa-flag.jpg
        )
)

I could not be able to use sort on both keys to synchronize the country name with country flag.

1

2 Answers 2

4

Please find below solution

$kd = array(
'name' => array(
        '0' => 'South Africa',
        '1' => 'Australia',
        '2' => 'Egypt',
    ),

'img' => array
    (
        '0' => 'sa-flag.jpg',
        '2' => 'au-flag.jpg',
        '1' => 'eg-flag.jpg',
    ),
);
array_multisort($kd['name'], SORT_ASC, SORT_STRING,$kd['img'], SORT_ASC, SORT_STRING);
echo '<pre>';
print_r($kd);

Find below core concept

http://www.php.net/manual/en/function.array-multisort.php#example-4840

1
  • 1
    +1 nice answer but you can also add reference so it looks great answer.
    – Tony Stark
    Commented Apr 25, 2013 at 8:10
0

Try this

$arr = array_multisort($array);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.