简体   繁体   中英

PHP Parsing url query to two dimensional array

I have a url query like this: units[]=3&grade[]=1.75&units[]=2&grade[]=1.50

What I need is have output something like this:

UNIT 1   -   3
GRADE 1  -   1.75

UNIT 2   -   2
GRADE 2  -   1.50

In my code, it displays something like this:

UNIT 1   -  3
GRADE 1  -  2

UNIT 2   -  1.75
GRADE 2  -  1.50

Does anyone know how to fix my code?

This is my code:

<?php
$get_string = $_SERVER['QUERY_STRING'];
$get_string =  urldecode($get_string);
parse_str($get_string, $get_array);
$colors = $get_array;

$i = 1;
foreach ($colors as $key => $value) {
    echo "UNIT ".$i." - ".$value[0]."<BR>";
    echo "GRADE ".$i." - ".$value[1]."<BR><BR>";
    $i++;
}
?>

The format of $colors array actually is this:

array(2) {
  ["units"]=>
  array(2) {
    [0]=>
    string(1) "3"
    [1]=>
    string(1) "2"
  }
  ["grade"]=>
  array(2) {
    [0]=>
    string(4) "1.75"
    [1]=>
    string(4) "1.50"
  }
}

To output the way you want code could be like this:

<?php
$get_string = "units[]=3&grade[]=1.75&units[]=2&grade[]=1.50";
$get_string =  urldecode($get_string);
parse_str($get_string, $get_array);
$colors = $get_array;

$units = $colors['units'];
$grades = $colors['grade'];

for ($i = 0; $i < count($units); $i++) {
    echo "UNIT ".$i." - ".$units[$i]."<BR>";
    echo "GRADE ".$i." - ".$grades[$i]."<BR><BR>";
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM