Reputation: 1
despite my efforts I wasn't able to find a suitable solution. Here is the problem:
All the data comes from a form with text fields named name[]
, gender[]
, and age[]
.
print_r($_POST)
looks like:
[name] => Array ([2] => Adam [6] => Suzy )
[gender] => Array ( [2] => male [6] => female )
[age] => Array ( [2] => 30 [6] => 25 )
I am trying to iterate it like this:
foreach ($array as $value)
{
echo $value['name'].$value['gender'].$value['age']."<br>";
}
The result should look like this:
Adam - male - 30
Suzy - female - 25
Upvotes: 0
Views: 196
Reputation: 1
@Being Sunny Veeeery close to that sir I'v used back in 2003. Here is the working solution:
<?
echo "<pre>";
print_r($_POST);
echo "</pre>";
foreach ($_POST['name'] as $key => $name) {
echo "NAME=".$_POST['name'][$key]."gender=" . $_POST['gender'][$key] . "AGE=".$_POST['age'][$key] . "<br>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
WOKSING SOLUTION:
<code>echo "<pre>";
print_r($_POST);
echo "</pre>";
foreach ($_POST['name'] as $key => $name) {
echo "NAME=".$_POST['nemae'][$key]."gender=" . $_POST['gender'][$key] . "AGE=".$_POST['age'][$key] . "<br>";
}
</code>
<form method="post">
<input type="text" name="name[]" />
<input type="text" name="gender[]"/>
<input type="text" name="age[]"/>
<br />
<input type="text" name="name[]" />
<input type="text" name="gender[]"/>
<input type="text" name="age[]"/>
<input type="submit" />
</form>
</body>
</html>
Upvotes: 0
Reputation: 38502
First of all I've modified the array structure that you posted on your question because it is not valid for in php
. Then If I don't misunderstood you requirements then you've this array structure and you want to archive this-
<?php
$array = array (
'name'=>array('Adam', 'Suzy'),
'gender'=>array('male', 'female'),
'age'=>array(30, 25)
);
$i=0;
foreach ($array as $key=>$value)
{
if($i==2)break;
echo $array['name'][$i]."-".$array['gender'][$i] ."-". $array['age'][$i] ."<br>";
$i++;
}
?>
OR
<?php
$array = array (
'name'=>array('Adam', 'Suzy'),
'gender'=>array('male', 'female'),
'age'=>array(30, 25)
);
foreach ($array['name'] as $index=>$name)
{
echo $name."-".$array['gender'][$index] ."-". $array['age'][$index] ."\n";
}
?>
Program Output:
Adam-male-30
Suzy-female-25
DEMO: https://eval.in/1039966
Upvotes: 0
Reputation: 41810
Each of the arrays in $_POST
have the same set of keys:
$_POST = array(
'name' => array(2 => 'Adam', 6 => 'Suzy'),
'gender' => array(2 => 'male', 6 => 'female'),
'age' => array(2 => '30', 6 => '25')
)
You can iterate one of the inner arrays, and use its key to access the corresponding values in the other arrays.
foreach ($_POST['name'] as $key => $name) {
echo $name . $_POST['gender'][$key] . $_POST['age'][$key] . "<br>";
}
Upvotes: 1
Reputation: 1332
You are close - but the syntax for creating arrays is slightly different.
$array = array (
array('name' => 'Adam', 'gender' => 'male', 'age' => 30),
array('name' => 'Suzy', 'gender' => 'female', 'age' => 25),
);
foreach ($array as $value)
{
echo $value['name'].$value['gender'].$value['age']."<br>";
}
You've got two options - you could create an array of two items; each has three details about a single person. That's what I did and it suits the loop you've shown.
Or you can have three parallel arrays - one with two names, one with two genders and one with two ages. That second way would look more like:
$array = array(
'name' => array('Adam','Suzy'),
'gender' => array('male','female'),
'age' => array(30,25)
);
But it would be harder to get the output you want from that.
$array2 = array(
'name' => array('Adam','Suzy'),
'gender' => array('male','female'),
'age' => array(30,25)
);
for($i=0;$i<count($array2['name']);$i++){
echo $array2['name'][$i].$array2['gender'][$i].$array2['age'][$i].'<br/>';
}
Upvotes: 2
Reputation: 377
foreach ($array as $id=>$value)
{
echo $value . $gender[$id] . $age[$id] . "<br>";
}
Upvotes: 0