Home  >  Article  >  Backend Development  >  How to pass array in the same PHP page

How to pass array in the same PHP page

PHPz
PHPzOriginal
2023-04-14 19:38:58780browse

As a web developer, we all know that PHP is a very powerful back-end language. During PHP development, we often need to pass data as an array. Passing arrays within the same PHP page is also a common requirement. Here I will explain how to implement array passing in the same PHP page.

1. Use global variables

In PHP, using global variables can help us pass arrays within the same page. We can define a new array at the top of the file and then change it inside a function or method. Other functions or methods can access the variable and obtain its latest state.

The sample code is as follows:

<?php
//定义一个全局的数组变量
$arr = array();

//函数内向数组中添加元素
function addToArray($value) {
    global $arr;
    $arr[] = $value;
}

//使用函数向数组中添加元素
addToArray(&#39;apple&#39;);
addToArray(&#39;banana&#39;);
addToArray(&#39;orange&#39;);

//遍历数组并输出
foreach ($arr as $value) {
    echo $value . &#39;<br>';
}
?>

As shown in the above code, we define a global array variable $arr and define a addToArray Function is used to add elements to the $arr array. When using the function, three elements are added to the $arr array. Finally, iterate over the array in the page and output its elements.

2. Use $_GET and $_POST arrays

Another implementation solution is to use the $_GET and $_POST that come with PHP array. Both arrays contain all data sent in the HTTP request. We can use $_GET and $_POST arrays in the same PHP page to pass array data.

The sample code is as follows:

<?php
if (isset($_GET[&#39;array&#39;])) {
    $arr = $_GET[&#39;array&#39;];
} else {
    $arr = array();
}
array_push($arr, &#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;);
?>

<form method="GET">
    <input type="hidden" name="array" value="<?php echo implode(&#39;,&#39;, $arr) ?>">
    <button type="submit">添加</button>
</form>

<?php
if (!empty($arr)) {
    foreach ($arr as $value) {
        echo $value . &#39;<br>';
    }
}
?>

As shown in the above code, we first check whether there is a key value named array in the $_GET array, If it exists, assign its value to the $arr variable. If it does not exist, an empty array is created.

We use the $_GET method in the form form and set up a hidden form element to pass array data to the same page. We concatenate the elements of the $arr array into a string through the implode() function and return it as the form element value.

When the user clicks the "Add" button, the form will be submitted. When submitting the form, we get the form element value and assign it to the $arr array. Finally, we use foreach to loop through the $arr array and output all elements.

Summary:

The above are two methods of passing arrays in the same PHP page. Arrays can be passed between the same file using the global variable method, and arrays can be passed within the page using the $_GET and $_POST arrays. Depending on the specific situation, we can choose the appropriate method to implement the transfer of the array.

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

Statement:
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