简体   繁体   中英

How to do a foreach loop in php with multiple array

There are 3 textareas in my form to collect name,age and place and echoing the output after submitting form.

Issue I am facing:

For example if I am entering data like below

Name :

George
Tom

Age:

30
40

Place:

SA
IN

I am expecting the output like:

My name is George, age 30, from SA
My name is Tom, age 40, from IN

But It giving me output:

My name is George, age 30, from SA
My name is George, age 30, from IN
My name is George, age 40, from SA
My name is George, age 40, from IN
My name is Tom, age 30, from SA
My name is Tom, age 30, from IN
My name is Tom, age 40, from SA
My name is Tom, age 40, from IN

How can I process each line data from each textarea and give the the output ?

My code: PHP FIDDLE

<form  id="main" name="main" action="#text" method="post" > 
<div class="input">Name<span><textarea  class="textarea" id="main-name" name="main-name" type="text"></textarea></span> </div>
<div class="input">Age<span><textarea  class="textarea" id="main-age" name="main-age" type="text"></textarea></span> </div>
<div class="input">Place<span><textarea  class="textarea" id="main-place" name="main-place" type="text"></textarea></span> </div>   


<div class="submit-clear">
            <input  id="generate" type="submit"  name="script" value="create my symcli script" />
            <input onClick="clear_all();"  id="clear" type="submit" name="clear" value="clear" />   
</div>
</form>



<div id="output-main">
<form id="main2" name="main2" action="#text" method="post">
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value=" ">

<?php
$names1 = $_POST['main-name'];
$ages1 = $_POST['main-age'];
$places1 = $_POST['main-place'];

$names = explode("\n", $names1); 
$ages = explode("\n", $ages1); 
$places = explode("\n", $places1); 

                foreach ($names as $key => $name) {
                foreach ($ages as $key => $age) {
                foreach ($places as $key => $place) {
                $name = trim($name); 
                $age = trim($age);
                $place = trim($place);


                echo "My name is $name, age $age, from $place".PHP_EOL;

                }}}


?>

</textarea>

</form>

</div>

foreach is the wrong loop structure here. What you actually do is just echoing every possible combination of entries.

Use a simple for loop so that every entry is just processed one time and not 2^(entries - 1) times (=> every possible combination).

$names = explode("\n", $_POST['main-name']);
$ages = explode("\n", $_POST['main-age']);
$places = explode("\n", $_POST['main-place']);

$entries = min(count($names), count($ages), count($places))

for ($i = 0; $i < $entries; $i++) {
    $name = trim($names[$i]); 
    $age = trim($ages[$i]);
    $place = trim($places[$i]);

    echo "My name is $name, age $age, from $place".PHP_EOL;
}

If you have same name, age and place value count. You can try this.

 foreach ($names as $key => $name) {
            $name = trim($name); 
            $age = trim($ages[$key]);
            $place = trim($places[$key]);


            echo "My name is $name, age $age, from $place".PHP_EOL;

           }

Otherwise you need to associate array like this array[0]['name'], array[0]['age'], array[0]['place']. This would be ideal option.

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