Jump to content

How to sort multiple arrays?


torvald_helmer

Recommended Posts

Or if both of the arrays hold single corresponding values, you might want to try using an associative array, and then ksort() it.

 

<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?> 

changes the array to be sort by the key. If you want it sorted by the value, use asort($fruits) instead. Output is

a = orange

b = banana

c = apple

d = lemon

That's taken from the php.net page on ksort, http://www.php.net/manual/en/function.ksort.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.