0

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

6
  • 2
    where you got this php array structure ? Commented Jul 20, 2018 at 16:35
  • Is it really multidimensional array ??? Commented Jul 20, 2018 at 16:40
  • 1
    Can you explain more about what you're starting with? $array as you have it written will not parse. Is this your actual code? Are you asking how to define the array, or do you already have an array (somewhat) like what you've shown and you're asking how to iterate/output it? Commented Jul 20, 2018 at 16:42
  • Lets say I have a from with multiple rows. Each row is containing 3 text fields filled with name, gender and age. by select few of them and post for processing I am getting the arrays I posted. I solved this question once 5-6 yrs ago but cant remember how :) Commented Jul 20, 2018 at 16:59
  • @MiroArsov I updated the question with the info from your comments. Please feel free to re-edit if it's not to your liking. Commented Jul 20, 2018 at 17:00

5 Answers 5

2

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/>';
}
Sign up to request clarification or add additional context in comments.

4 Comments

We should be encourging people to use the new(ish) array syntax imo. $array = ['value1', 'value2'];
Thank you for your answer! All the data comes from a form with text fields and the arrays are name[], gender[], and age[].
[name] => Array ( [2] => Adam [6] => Suzy ) [gender] => Array ( [2] => male [6] => female ) [age] => Array ( [2] => 30 [6] => 25 ) This is exactly how my print_r($_POST) looks like!
Yep, that one was tested about an hr ago. Doesnt work... code [name] => Array ( [2] => Adam [6] => Suzy ) [gender] => Array ( [2] => male [6] => female ) [age] => Array ( [2] => 30 [6] => 25 ) code
1

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>";
}

8 Comments

Not working in what way? What does it do? Are there errors?
I tried out the link. The $_POST values show up as expected, and I don't see any errors, but there's no output besides that when submitting the form. Are you using the foreach loop from this answer on that page, or something else?
<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>
<? echo "<pre>"; print_r($_POST); echo "</pre>"; $array2 = array( $name,$gender, $age ); for($i=0;$i<count($array2['name']);$i++){ echo $array2['name'][$i].$array2['gender'][$i].$array2['age'][$i].'<br/>'; } ?>
Tell me what is this..... I am writing since 2002(ocasionaly) and never had such issu. The problem can be solved "dumb" way of course!
|
0
foreach ($array as $id=>$value)
{
    echo $value . $gender[$id] . $age[$id] . "<br>";
}

Comments

0

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

Comments

0

@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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.