REAL排序没有循环的数组

时间:2014-02-13 20:20:36

标签: php arrays loops echo asort

我正在寻找排序没有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);
?>

谢谢!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

这是你可以做到的一种方式:

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

应该给你这个输出:

1 = apple
2 = banana
3 = lemon
4 = orange
相关问题