Question

I'm looking for sort an array WITHOUT foreach loop (direct command)...

for example :

<?php

$sortme = array( 10, 8, 17, 6, 22, 4, 3, 87, 1);

asort($sortme);

echo $sortme[0]; //Why this is not the lowest value (which is 1 on this case) ?!

//Is there any direct command sort array WITHOUT foreach loop ?

// iow...
// I need this :
// $sortme = array( 10, 8, 17, 6, 22, 4, 3, 87, 1);
// (here is the magic command) to become this :
// $sortme = array( 1, 3, 4, 6, 8, 10, 17, 22, 87);
?>

Thanks !

No correct solution

OTHER TIPS

sounds like you just need sort()

<?php
    $sortme = array( 10, 8, 17, 6, 22, 4, 3, 87, 1);
    sort($sortme);
    echo '<pre>';
    print_r($sortme);
    echo '</pre>';
    echo 'First: '.$sortme[0];
?>

Result :

Array
(
    [0] => 1
    [1] => 3
    [2] => 4
    [3] => 6
    [4] => 8
    [5] => 10
    [6] => 17
    [7] => 22
    [8] => 87
)
First: 1

Here's one way you could do this:

<?php
$fruits = array("3" => "lemon", "4" => "orange", "2" => "banana", "1" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

Should give you this output:

1 = apple
2 = banana
3 = lemon
4 = orange
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top