开发者

How do i sort the array Key items in php

foreach($sql_result['record2'] as $key ){
             echo  $key['ENO'];
}

when iam doing foreach loop for the above statement .It outputs (10,9,2,8,4).I开发者_开发百科 need it to sort to (2,4,8,9,10) one more thing is "$key" is of Type Array.when i do like this array_multisort($key['ENO']). how do i acheive this


$vals = array();
foreach($sql_result['record2'] as $key ){
    $vals[] = $key['ENO']
}
sort($vals);

or if you want to pre-sort the values you could use usort()


Untested Code, but something like the following should work....

$tmpArray = array();
foreach ($sql_result['record2'] as $key => $value) {
    array_push($tmpArray, $value['ENO']);
}

array_multisort($tmpArray, SORT_DESC);

At this point, $tmpArray will be your sorted array of values. OR, you can just do an ORDER BY clause in your SQL, in which case the result set will be ordered by the values you want.


You don't need to sort the array, you can simply append an "ORDER by ENO" to your SQL query to return an ordered result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜