1
foreach ($_GET['first_name'] as $first_name) {
   echo $first_name . '<br>';
}

foreach ($_GET['surname'] as $surname) {
   echo $surname . '<br>';
} 

foreach ($_GET['age'] as $age) {
   echo $age . '<br>';
} 

foreach ($_GET['gender'] as $gender) {
   echo $gender . '<br>';
} 

The code above returns:

All firstnames

All surnames

All ages

All genders

I want it to look like this:

Firstname Surname

Age

Gender

...

[next person]

I have tried to resolve it this way:

$names = array_combine($_GET['first_name'], $_GET['surname']);
foreach($names as $firstname => $surname) {
    echo $firstname . ' ' . $surname . '<br>';
}

This would fix my problem for firstname and surname but I still wouldn't know how to handle the other arrays.

Pom Canys
  • 369
  • 2
  • 5
  • 16

3 Answers3

5

Just run a loop with an incrementing variable and use that to specify the index of the arrays in each iteration.

Like this:

for($i=0;$i<count($_GET['first_name']);$i++) {
    echo $_GET['first_name'][$i] . ' ' . $_GET['surname'][$i] . '<br>';
    //...etc
}
Geoff Atkins
  • 1,693
  • 1
  • 17
  • 23
2

Try this for your desired output:

foreach ($_GET['first_name'] as $id => $key) {

    echo $_GET['first_name'][$id]." ".$_GET['surname'][$id]." ".$_GET['age'][$id]." ".$_GET['gender'][$id]."<br/>";
}
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
0

Try:

array_merge($array1, $array2)

Found on: http://php.net/manual/es/function.array-merge.php

SuperDJ
  • 7,488
  • 11
  • 40
  • 74
fito
  • 116
  • 9