REAL 对数组进行排序,无需循环



我正在寻找在没有foreach循环的情况下对数组进行排序(直接命令)...

例如:

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

谢谢!

听起来你只需要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];
?>

结果:

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

这里有一种方法可以做到这一点:

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

应该给你这个输出:

1 = apple
2 = banana
3 = lemon
4 = orange

相关内容

  • 没有找到相关文章

最新更新