Passing PHP Arrays

How to pass php variable arrays between pages.
Author: Author: D.Shaun Morgan
See Also:
How can I pass a PHP variable from one page to another

In PHP programming, variable arrays can be used to pass data from a form to a php script or it may be used to store and move data around inside of script.

The data usually collected with website forms then sent to a a php script for processing. After processing the data will be either sent back to a web page and the user or it might be stored in a database or file on the server.

Php does not require variable types like some other languages - ie:

int x, char y;

Therefore,they come in two main types - "arrays" and "non arrays."
This includes object variables also since objects are fundamentally arrays.

Single non-array variables work best with scripts that handle small amounts of information - such as a single text-box of input.

On the other hand when you are working with forms that handle multiple boxes of user input, you may benefit from using a variable array instead of writing individual variables.

Creating individual variables for just 4 or 5 text-boxes works out OK, but what about a customer database with hundreds or even thousands of inputs text boxes?

The task of typing all those variables would be time consuming and make for hard to manage code. This is where arrays can make difference.

Here we have five individual non-array variables

Example No. 1.

Notice the values in the input tag's "name" attribute - first_name0 through first_name4(Form example No. 1). These will be automatically converted into $_POST array keys facilitating access to text-box information.
Example No. 2. demonstrates how to access those variables via php script. The $_POST variables are global and can be accessed from every page on your website.

Example No. 2.

Let's Take a Look at Passing PHP arrays

In example No. 1. we used 5 different variables.

Change the "name" attribute values to "first_name[] - adding the square brackets to the end of the variable name.

Example No. 2

When submitting (Example No. 2) values will be saved in a single dimensional array "first_name" then automatically placed inside the $_POST super global.

Example No. 3 - Script to Access Php Array

Important Observation Concerning Square Brackets

If you forget to add the brackets - ie: "first_name" versus "first_name[]",

only the last text-box will be submitted.
Don't forget your brackets!

Php Array's and Functions

Another useful technique involving php arrays is using them to return information from inside of a function.

PHP functions can take several arguments but they only return one value.

However, that value CAN be an array, and that array can contain several important values.

Save Time With Loops

What if there is a need for a thousand text boxes? Won't I have write a thousand separate text-boxes AND variable names in the "name" attribute?"

That is where Loops come in to play!

Php can be used to dynamically generate HTML.

The next script will output five input text boxes.

SCript output

Array Syntax - Things to Note

PHP syntax requires right and left square brackets to be placed after the variable name - $var_name"[]" .

Array Key and Index are important concepts to understand. They are how you access array data.

The key and index are located in between the square brackets.

An array index is a numerical value starting at zero.


An array key is a string value placed between quotation marks - placed between square brackets.


The array key can also be a variable.


The "key" is a named location in memory that stores data via php array. This provides a human readable method to access and work on data.

Click here for a visual model PHP $_POST.

Author: D.Shaun Morgan