How to accept form array in php

王林
Release: 2023-05-19 13:17:39
Original
721 people have browsed it

In PHP, a form can not only submit a single data, but also submit multiple data in the form of an array. When dealing with this situation, we need to use some of PHP's built-in functions to receive and process these arrays. This article will introduce how to use PHP to receive and process form arrays.

First, let's take a look at how to submit an array in the form:

<form action="submit.php" method="post">
  <input type="text" name="name[]" value="Alice">
  <input type="text" name="name[]" value="Bob">
  <input type="text" name="name[]" value="Charlie">
  <input type="submit" name="submit" value="Submit">
</form>
Copy after login

In the above form, we have used the same name "name" in the name attribute of each input field []", which means that the values ​​of these input fields will be submitted as an array named "name".

Next, we need to receive this array in the PHP file. We can use the $_POST array to get this array and use PHP built-in functions to process it. Let's look at a few examples.

Get the entire array

If you want to get the entire submitted array, you can use the $_POST array to get it. In the above example, we can use the following code to get the entire array:

$name_array = $_POST['name'];
Copy after login

Here, we assign the submitted name array to the variable $name_array. Now, $name_array will contain all values ​​submitted. If you want to see the contents of this array, you can use the print_r function:

print_r($name_array);
Copy after login

This will output the following:

Array
(
    [0] => Alice
    [1] => Bob
    [2] => Charlie
)
Copy after login

Get a specific array element

If you want To get a specific element of the submitted array, you can use a subscript to get it. For example, if you want to get the first element of an array, you can use the following syntax:

$first_element = $_POST['name'][0];
Copy after login

Here, we use subscript "0" to get the first element of the array. Likewise, you can use other subscripts to get any element of the submission.

Loop through the array

If you want to traverse and process the entire array, you can use PHP's built-in foreach statement. The following is a sample code:

foreach ($_POST['name'] as $name) {
  echo $name . "<br>";
}
Copy after login

Here, we use the foreach statement to iterate through the array named "name" and assign each element to the variable $name. In this example, we are simply outputting the value of each element, but you can use any regular PHP code in a foreach statement to manipulate the array.

Conclusion

As mentioned above, receiving and processing form arrays using PHP is very simple. You can use $_POST array to get the array and use PHP built-in functions to process it. Whether you want to get the entire array or specific array elements, or iterate through and process the entire array, PHP provides a variety of methods to meet your needs.

The above is the detailed content of How to accept form array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template