Jak pobrać tablicę wejściową formularza do tablicy PHP

Mam taki formularz jak ten poniżej, który jest zamieszczony w kontaktach.php, a użytkownik może dynamicznie dodawać więcej za pomocą jquery.

<input type="text" name="name[]" />
<input type="text" name="email[]" />

<input type="text" name="name[]" />
<input type="text" name="email[]" />

<input type="text" name="name[]" />
<input type="text" name="email[]" />

Jeśli echo je w php z kodem poniżej

$name = $_POST['name'];
$email = $_POST['account'];

foreach( $name as $v ) {
print $v;
}

foreach( $email as $v ) {
print $v;
}

Dam coś takiego:

Name1name2name3email1email2email3

Jak mogę umieścić te tablice w czymś takim jak poniższy kod

function show_Names($n, $m)
{
return("The name is $n and email is $m, thank you");
}

$a = array("name1", "name2", "name3");
$b = array("email1", "email2", "email3");

$c = array_map("show_Names", $a, $b);
print_r($c);

Więc moje wyjście jest takie:

Nazwa to Nazwa1 a email to email1 , thank you
Nazwa to name2 a email to email2 , dziękujemy
Nazwa to name3 a email to email3 , dziękujemy

Dziękuję za pomoc lub Radę

Author: osullic, 2010-07-23

8 answers

Są już w tablicach: $name jest tablicą, podobnie jak $email

Więc wszystko, co musisz zrobić, to dodać trochę przetwarzania, aby zaatakować obie tablice:

$name = $_POST['name'];
$email = $_POST['account'];

foreach( $name as $key => $n ) {
  print "The name is ".$n." and email is ".$email[$key].", thank you\n";
}

Aby obsłużyć więcej wejść, wystarczy rozszerzyć wzór:

$name = $_POST['name'];
$email = $_POST['account'];
$location = $_POST['location'];

foreach( $name as $key => $n ) {
  print "The name is ".$n.", email is ".$email[$key].
        ", and location is ".$location[$key].". Thank you\n";
}
 124
Author: Jeffrey Blake,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-04 18:16:22

Np. poprzez nazwanie pól typu

<input type="text" name="item[0][name]" />
<input type="text" name="item[0][email]" />

<input type="text" name="item[1][name]" />
<input type="text" name="item[1][email]" />

<input type="text" name="item[2][name]" />
<input type="text" name="item[2][email]" />

(co jest również możliwe przy dodawaniu elementów przez javascript)

Odpowiedni skrypt php może wyglądać tak:

function show_Names($e)
{
  return "The name is $e[name] and email is $e[email], thank you";
}

$c = array_map("show_Names", $_POST['item']);
print_r($c);
 39
Author: VolkerK,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-23 00:38:36

A jeśli masz zestaw pól?

<fieldset>
<input type="text" name="item[1]" />
<input type="text" name="item[2]" />
<input type="hidden" name="fset[]"/>
</fieldset>

<fieldset>
<input type="text" name="item[3]" />
<input type="text" name="item[4]" />
<input type="hidden" name="fset[]"/>
</fieldset>

Dodałem ukryte pole, aby policzyć liczbę zestawów pól. Użytkownik może dodać lub usunąć pola, a następnie je zapisać.

 2
Author: Sophia Gavish,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-25 16:06:28

Wiem, że jest już trochę późno, ale mógłbyś zrobić coś takiego:

function AddToArray ($post_information) {
    //Create the return array
    $return = array();
    //Iterate through the array passed
    foreach ($post_information as $key => $value) {
        //Append the key and value to the array, e.g.
            //$_POST['keys'] = "values" would be in the array as "keys"=>"values"
        $return[$key] = $value;
    }
    //Return the created array
    return $return;
}

Test z:

if (isset($_POST['submit'])) {
    var_dump(AddToArray($_POST));
}

This For me produced:

array (size=1)
  0 => 
    array (size=5)
      'stake' => string '0' (length=1)
      'odds' => string '' (length=0)
      'ew' => string 'false' (length=5)
      'ew_deduction' => string '' (length=0)
      'submit' => string 'Open' (length=4)
 2
Author: Sam Swift 웃,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-10 14:51:13

Ja też natknąłem się na ten problem. Podane 3 wejścia: field [], field2 [], field3 []

Możesz uzyskać dostęp do każdego z tych pól dynamicznie. Ponieważ każde pole będzie tablicą, wszystkie powiązane pola będą miały ten sam klucz tablicy. Na przykład podane dane wejściowe:

Bob i jego e-mail i płeć będą dzielić ten sam klucz. Mając to na uwadze, możesz uzyskać dostęp do danych w pętli for, takiej jak to:

    for($x = 0; $x < count($first_name); $x++ )
    {
        echo $first_name[$x];
        echo $email[$x];
        echo $sex[$x];
        echo "<br/>";
    }

To również skaluje. Wszystko, co musisz zrobić, to dodać odpowiednie var-y tablic za każdym razem, gdy chcesz dodać nowe pola.

 1
Author: thatonefreeman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-07 13:43:52

Jednak rozwiązanie VolkerK jest najlepsze, aby uniknąć miss para między e-mail i nazwy użytkownika. Więc musisz wygenerować kod HTML za pomocą PHP w następujący sposób:

<? foreach ($i = 0; $i < $total_data; $i++) : ?>
    <input type="text" name="name[<?= $i ?>]" />
    <input type="text" name="email[<?= $i ?>]" />
<? endforeach; ?>

Zmień $total_data do własnych potrzeb. Aby to pokazać, tak po prostu:

$output = array_map(create_function('$name, $email', 'return "The name is $name and email is $email, thank you.";'), $_POST['name'], $_POST['email']);
echo implode('<br>', $output);

Zakładając, że dane zostały wysłane metodą POST.

 0
Author: iroel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-23 06:30:46

Niemniej jednak, możesz użyć poniższego kodu jako,

$a = array('name1','name2','name3');
$b = array('email1','email2','email3');

function f($a,$b){
    return "The name is $a and email is $b, thank you";
}

$c = array_map('f', $a, $b);

//echoing the result

foreach ($c as $val) {
    echo $val.'<br>';
}
 0
Author: Abhishek Gurjar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-13 10:33:03

Użycie tej metody powinno zadziałać:

$name = $_POST['name'];
$email = $_POST['account'];
while($explore=each($email)) {
    echo $explore['key'];
    echo "-";
    echo $explore['value'];
    echo "<br/>";
}
 -3
Author: Raphael Gates,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-23 22:23:42