How to sort an array by keys in an ascending direction?

梦想的初衷 提交于 2021-02-10 20:02:01

问题


here is the input i am getting from my flash file

process.php?Q2=898&Aa=Grade1&Tim=0%3A0%3A12&Q1=908&Bb=lkj&Q4=jhj&Q3=08&Cc=North%20America&Q0=1

and in php i use this code foreach ($_GET as $field => $label) { $datarray[]=$_GET[$field];

echo  "$field :";
echo $_GET[$field];;
echo "<br>";

i get this out put

Q2 :898 Aa :Grade1 Tim :0:0:12 Q1 :908 Bb :lkj Q4 :jhj Q3 :08 Cc :North America Q0 :1

now my question is how do i sort it alphabaticaly so it should look like this Aa :Grade1 Bb :lkj Cc :North America Q0 :1 Q1 :908

and so on....before i can insert it into the DB


回答1:


ksort($_GET);

This should ksort the $_GET array by it's keys. krsort for reverse order.




回答2:


what you're looking for is ksort. Dig the PHP manual! ;)




回答3:


To get a natural sort by key:

function knatsort(&$karr){
    $kkeyarr = array_keys($karr);
    natsort($kkeyarr);
    $ksortedarr = array();
    foreach($kkeyarr as $kcurrkey){
        $ksortedarr[$kcurrkey] = $karr[$kcurrkey];
    }
    $karr = $ksortedarr;
    return true;
}

Thanks, PHP Manual!

foreach ($_GET as $key => $value) {
 echo $key.' - '.$value.'<br/>';
}


来源:https://stackoverflow.com/questions/85770/how-to-sort-an-array-by-keys-in-an-ascending-direction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!