Learn PHP
Site Map
Php Functions
C++ - Starting Out
Web Design Tools
Photoshop Tutorials
Droid Phones
Building Websites
ArrowTech Blog
Web Servers
Making Money
Favorite Links
Privacy Policy

Category: php


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

convert timestamp to readable date time using php Convert Unix Timestamp to Readable Date Time PHP User Function string function convert_timestamp(string $file_in

creating image watermarks Creating PHP Watermarks $my_image = imagecreatefromjpeg('photo.jpeg'); $watermark = imagecreatetruecolor(100

need to print out an array using php How toPrint PHP Arrays Things to Consider Are: Array Type- Single Array or Multidimensional Array


odd number php script Testing for Odd/Even Numbers - PHP Example Banner Rotation PHP Script Odd/Even Numbers The easiest way to setup an Odd / Even test using PHP i

how to upload images and files using php Upload Images to Webserver HTML Form PHP Function # UPLOAD FILE function upload_file(){ echo '

using php cookies variables Using Cookie Variables in PHP Setting cookies in php is pretty straight forward. There is a php function: setcookie(); This function mu

how to unzip files on your free hosting account Uzipping Files on Your Web Server If you are using a free hosting account with godaddy.com and many of the other Free hosts out there, you may have found that they don't offer you a lot o

passing data from one form to another form using php Passing data from one form to another form To pass php data from a form and then catch that data into another form one way to accomplish this task is to intermi

Just In Time Compiling JIT - Just in Time Compilers JIT refers to a special software that is found in web-browsers, and scripting engines such PHP's Zend. (My understanding of ZEND is that it

Storing image locations mysql Store Image URL in Database Retrieve Image with PHP Script

how to turn variable value into variable name Variable Variables in PHP Every so often when you are writing a php file, we all run into a situation where we would like for the name of the string that is inside

what-is-php Explanation of PHP Basic Syntax --New to PHP Php is a language that is very much like writing c. The best feature of PHP versus c is that it is designed pri

how to use dreamweaver Dreamweaver - WISIWIG Dreamweaver is a tool that arguably could be called the industry standard for web design . Dreamweaver, is an Adobe product that originally b

Filezilla Filezilla Ftp Client, FTP SERVER vs Wsftp By: D.Shaun Morgan While I was writing this article, about halfway through, I realized that it would be unfair to tell my readers about

notepad plus Notepad++ Download Notepad Plus Plus Notepad plus plus is an open source, free text editor. It is

how to open create write files using php Php file functions, Opening a file in php, reading a file in php, writing a file in php In this tutorial we will look at the most commonly used file functions used in php. The

stop form spam captcha PHP -- How to Protect My Email Form / Contact Form Against Spam Bots Author: D.Shaun Morgan Versions and Skill Level PHP 5

retreive images mysql using php How to get images out of mysql database with php and use them on my webpages Author: D.Shaun Morgan PHP Version - PHP 5x

how to make money online Making Money on the Internet Sign up for this series of free money making articles Name:

store images mysql using php Storing Images in Mysql Database Tutorial Outline: Creating a Test Mysql Database Example php Database Functio

php example post array Visual example of how PHP handles the super-global array variable "$_POST" Author: D.Shaun Morgan Versions and Skill Level

php arrays Passing PHP Arrays How to pass php variable arrays between pages. Author:

how to write php functions Explanation of a Basic PHP Function What is a PHP function? In PHP a function represents a block of instructions that perform

how to write html form using php When learning PHP to build a website, one of the very first things that any newcomer, or beginner needs to learn is how to use HTML or XHTML (both will mean the same for this tutorial.) fo

register globals long arrays Security, php.ini, register_globals and register_long_arrays Author: D.Shaun Morgan Versions and Skill Level PHP Version - PHP 5x

how to install php 5 How to Install PHP 5.x.x.x on Windows This is beginner's how-to manual explaining how to quickly setup a PHP 5 installation on a Windows PC. You must have an Apache webserver installed

php passing variables Pass Variables from One Page to Another -- PHP Author: D.Shaun Morgan Tutorial Outline : Passing PHP variables with $_


Leave a Comment


Name:      | Email:      | Website:



Enter The Above Security Code:

Author: Kevin Davis | Website URL: http://kevincdavis.net/ |
Nice script, but I do have a quick question. How I can make it work in where number of fields I need is unknown. Say for example someone needs at least 10 fields instead of 5, how can I make it work that way?

Re: From Admin:

If I am understanding you correctly, what you are wanting to know is how to deal with an array of variables that could have variable changing amount of data in it.

If, however, you need a webpage that offers your visitors a variable amount of input text boxes, you will want to write a script and a page that asks the user if he wants to add another box or field. It might look somethign like this:


if($_GET['anotherBox'] == "y"){
$newbox = "<input type='text' name='anotherBox[]' value='' /> <br/><br/> "
} $_SESSION["$AllBoxes"] .= $newbox;

Then further down in the page you can tell the all boxes variable to dynamically produce another input box every time your user chooses to create one.

<form action="" method="get" > <br/>

<php echo $_SESSION["$AllBoxes"]; ?>
<input type="checkbox" name="anotherBox" value="y"/> <br/><br/>
<input type="submit" value="Submit" /><br/><br/>

After the user fills in his buy web hosting or other eccomerce form and puts in all the data he needs, there will exist on the webserver where you went to buy hosting and but a domain domain anem a $_GET variable array called $_GET['anotherBox'] that will hold the data from the dynamic form. In this case you will use something like a "for loop" to use the data in that variable
Author: Roberto Molina | Website URL: |
Hello I have other situation and I need to pass a lot of data from several pages or Formula maybe 8 to another with all the data. Let me give a Example:

We have one Formula with, name Age Address and a small calculation, and then I want to have only one report with all these data. That means all the 8 People in only one Table. The formula is building with radio button and text Box. I can do only with one formula and one report, but with 8 fields to connect with one Report I don't have any idea. Many thanks.
Author: developer99 | Website URL: http://developer99.blogspot.com |
hi, you can pass array through two methods. by input values or by json,, i hope that this post will help you http: developer99.blogspot.com 2011 07 passing-array-from-one-page-to-another.html
Author: ward | Website URL: www.telerex-europe.com |
Hello, How do I do the complete opposite of this tutorial? I have a given number of values out of a select , generated by a FOR loop: Through this I m able to generate a string with DOCUMENT.WRITELN, but I want to get this string into a TEXTBOX, without leaving the page... After completing the form which has more values to enter I want to pass this TEXTBOX value as a single string to the database... function selIt { var pickList = document.form1.elements[ list2[] ] var pickOptions = pickList.options var pickOLength = pickOptions.length for var i = 0 i pickOLength i++ { document.writeln pickOptions[i].value } return true } How can this be done? Thanks in advance, Bye Ward
Author: Tupa | Website URL: |
This code:

for $x=0 $x 4 $x++ {}

Actually output four input text boxes, not five.

Re: Admin

I'm certain that it does output 5

Re: Admin

Actually you caught it Jeffry. It does now create five textboxes.
Author: Cameron Freck | Website URL: |
G day great example, just wondering is it possible to use multi-dimensional arrays with this?

Re: Admin
PHP does support multi-demensional arrays. However, I have always found that they come with a serious performance hit.