1

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

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
hitek
  • 372
  • 1
  • 17
  • 33

3 Answers3

6
ksort($_GET);

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

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
terminus
  • 13,745
  • 8
  • 34
  • 37
  • `ksort()` will indeed do the job, but it is not advisable to modify POST/GET variables, even if it is only to re-order them. A workaround would be `$get = $_GET;` then manipulate the custom array instead. – rybo111 Mar 08 '16 at 11:19
1

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

Leonid Shevtsov
  • 14,024
  • 9
  • 51
  • 82
  • Posting a hurried link to the documentation perfectly represents how FGITW answers were bad for Stack Overflow and its researchers during the early years. This answer lacks generosity. – mickmackusa Feb 19 '23 at 21:34
0

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/>';
}
micahwittman
  • 12,356
  • 2
  • 32
  • 37