Multi-dimensional Arrays in PHP
12,264 ViewsPHP Tutorials May 21st, 2007
In this php tutorial, we will learn how to create multidimensional arrays in PHP, we will be creating a form, and another page to handle the form to demonstrate the use of multi-dimensional arrays in PHP.
We want to start off by creating a new HTML document.
This is just some basic HTML code, if you dont understand it then you may want to go read some HTML tutorials because it is the most basic "coding" you will find anywhere.
Now we begin our HTML form.
Our form will use handle_form.php to send all it's data to be processed to. The first input in our form is going to be the users Name, so we create a text input and give it a name=" " of name.
Now we need to create a series of check boxes so that users can select what there interests are.
-
<b>Interests:</b>
-
<input type="checkbox" name="interests[]" value="Music" /> Music
-
<input type="checkbox" name="interests[]" value="Movies" /> Movies
-
<input type="checkbox" name="interests[]" value="Sports" /> Sports
-
<input type="checkbox" name="interests[]" value="Reading" /> Reading
-
<input type="checkbox" name="interests[]" value="Relaxing" /> Relaxing
-
</p>
In the interests section of the form, the user is given the choice to select multiple interests using a checkbox. Instead of giving the checkbox the same name as it's value, we are going to give it the name of interests[ ] so that whatever the user chooses will be put into the interests[ ] array which we will later on be able to access by using the $_POST['interests'] global variable that will have all there selections saved.
Now we just need to finish off our document by closing off all of our tags.
Save this file as about.html.
Now we need to create handle_form.php since that is where our form submits all of it's information, and in this file is where we will use the $_POST['interests'] multidimensional array. So we start off by creating a new HTML document.
-
<html><head>
-
<title>Your form feedback</title>
-
</head>
-
<body> <?php
Now we need to validate that they actually entered in there name.
-
else
-
{ $name = NULL; echo '<p><font color="red">Please go back and enter your name into the form.</font></p>'; }
Now we validate the users interests.
This piece of code validate that the user actually selected at least one of the interests on the previous page, if they did then the variable $interests is set to TRUE, if they selected none of them then $variables is set to NULL.
Now we just need to start our final conditional statements.
-
if ($name && $interests)
If the form was filled out properly both the $name and $interests varialbe will be set to TRUE, this last conditional statement just checks if both variables are set to TRUE, if they are, then you can continue.
Now we need to print out the selected interests from the previous page.
-
foreach ($_POST['interests'] as $value)
To access the interests from the previous page we will use a foreach loop, we will print out the array $_POST['interests'] using this foreach loop. Seeing as how checkboxes only have a value if they are selected, then this will only print out the selections made by the user. Now we need to finalize that final conditional.
The first echo ends the first <ul> tag, then we just create an else statement so that if there were any errors in the form it will just tell the user to go back and fill it in properly. Now we just end off our PHP document, and our HTML document.
-
?> </body> </html>
Save the file as handle_form.php, upload both about.html and handle_form.php to your web server and test it out!
The complete php script code is below:
-
<html><head>
-
<title>Your form feedback</title>
-
</head>
-
<body> <?php
-
-
else
-
{ $name = NULL; echo '<p><font color="red">Please go back and enter your name into the form.</font></p>'; }
-
-
else
-
{ $interests = NULL; echo '<p><font color="red">Please go back and enter in your interests into the form.</font></p>'; }
-
-
-
if ($name && $interests)
-
-
foreach ($_POST['interests'] as $value)
-
-
-
?> </body> </html>
I hope you enjoyed this tutorial, if you need any help with it don't hesitate to contact us.

(9 votes, average: 4.44 out of 5)










June 6th, 2007 at 6:56 am
thanks alot
June 9th, 2007 at 10:52 am
First of all , thank you for the tutorial , the fact that i'm reading this tutorial means that I can't or shold not be able to fix a mitake on your code , so that's when I start wasting time to understand why it's not running for me, so on line 8 ($_POST['name']_; }, shold be ($_POST['name']); } and Perfecto !! , please keep it in mind , if I am reading this , it means I can't deal with it yet. thank you again.
June 11th, 2007 at 11:20 pm
Thanks Daniel. Typo has been corrected.
June 14th, 2007 at 3:04 pm
Looked high and low for this answer. Others came close but yours worked. Thanks a ton!!!!!!
October 28th, 2007 at 5:23 pm
This is nice though it isn't multidimensional but single dimensional array example. Do you have any interests[][][]... example instead?
November 29th, 2007 at 4:01 am
Hi.
Good design, who make it?
February 3rd, 2008 at 11:10 am
can u guys gimme the idea how to implement a matrix in php?
so stuck with it...wargh!!
February 3rd, 2008 at 3:57 pm
thanks alot
June 10th, 2008 at 2:11 am
I can't see where this is a multi-dimensional array.
Sure, you're using the $_POST array, ie $_POST['name'] but a multi dimensional array would look somthing like:
Post Array
(
[myPostArray] => Array
( Value
[2] => Value
[3] => Value
) Value
[another button name] => value
)