PHP array | 9 demos of simple and usage with HTML elements

The purpose of arrays in PHP

The array is a type of variable used to store multiple values in PHP programs.

One of the advantages of arrays is, if you have to store similar type of information during the life of program in multiple variables, you may simply create a single array variable and store all values in it.

Another benefit is memory allocation for an array. If you create simple variables, these will be scattered in memory. In case of array, the items are stored in adjacent memory locations. This is simpler to deal with in a program as compared to scattered variables.

For example, your PHP program gets a list of fifty names from the user in a web form. As the user presses submit button to save or proceed to next step, you have to store those in PHP by using variables. One of the ways is to create variables for each name e.g. name1, name2 and so on.

The other way is to create an array of fifty elements e.g. name[].

In this tutorial of PHP array, I will show you working examples that include creating, accessing simple arrays and user sent data from web forms with other HTML elements, as well!

Creating arrays in PHP programs

This is how you can create arrays in PHP.

Creating an Array without keys:

 

A PHP array can contain both numbers and strings, e.g.:

 

Creating arrays with key / value pairs

 

I will show you the role of keys in demos while accessing arrays.

You may also create arrays like this:

 

PHP does not distinguish between numeric / indexed and associative arrays, so you can create arrays like this:

 

That means you can use a string as keys in array PHP.

In next section, I will show you examples with online demos and code to create, access and using different functions of PHP arrays. Some of the examples require user input to explain with HTML elements. Let me start with the basic examples.

Creating, accessing and printing an array

In the following example, an array of numbers is created with five items or elements. Then a foreach loop is used to access/print the array elements on the screen.

See the code and live example online by clicking the image or link below:

PHP array numbers

See online demo and code

This is how the array is created and used with the foreach loop:

 

The foreach loop is explained in its own chapter.

An example of key/value pair array

In this example, an array with key value pair is created and then printed by using the foreach.

PHP array key value

See online demo and code

Following PHP code is used to create and print the array with key/value pair:

 

A mixed array items example with accessing a specific element

In this example, an array of numbers and strings is created. After that, instead of using a foreach loop, I will access a specific array item as follows:

PHP array mixed items

See online demo and code

In the code of demo page, you can see array is created with mixed item i.e. numbers and strings. After that, the third array element is displayed by using its index key.

I used [2] for the third element, that means, the index of PHP arrays start at 0.

A simple associative array example

As such, PHP does not differentiate between simple indexed or associative arrays. The associative arrays are those in which strings are used as keys.

See the following demo online for having a better idea:

PHP associative array

See online demo and code

You can see, first an array is declared which is followed by creating its elements. As creating the array elements, I used strings as keys:

 

In the foreach loop, this time, I also accessed and displayed the array keys along with their respective items.

 

This is a useful feature, as such in bigger arrays this is really hard to remember or access array items based at numeric indexes. Having descriptive names as keys can be helpful in describing an array for the long term as well.

I will also show its usage in an advanced demo ahead.

An example of PHP array with HTML form

In some scenarios, you may need to ask users to enter similar kind of information, for example, enter ten names in given textboxes or some other data. You may need to store that information into the database as well.

In this example, five HTML textboxes are given to enter the names. As you click submit after filling the textboxes, those names will be displayed by using an array.

See demo online:

PHP array HTML form

See online demo and code

If you enter the names (or some dummy text) and press the submit button, you can see, the entered names are displayed.

So what is done is PHP code?

 

First of all, an array $txtnames is assigned the form’s textbox values. In the HTML form, I used the same name for each textbox with []:

 

As this is assigned to a PHP variable, this became an array of five elements. As we have an array now, we can use the same foreach loop as used in the above examples to print array elements.

Practically, you may want to get records from the user in an HTML form, for any other scenarios. As they submit, you may collect whole form’s data in PHP arrays and after establishing a connection to the database, you can insert records into the database table.

You may even use a form where HTML form fields are added on the fly to any number for entering data. See the following example.

An example of arrays with adding users in HTML form

In above example, only five textboxes were given where you may enter the names. In certain scenarios, it is up to the users to add as many records as they want. For example, adding users, products, orders etc.

This example is a combination of HTML form, jQuery’s method, and PHP arrays. As the demo page loads, only two textboxes are shown to enter “New user name” and “New user Email”.

Above that, a button is given, “Add More Users”. By clicking that, you can add as many textboxes as you want. After adding more textboxes to create users, fill the textboxes with some dummy data.

After that, press the Submit button where PHP arrays will be used to collect that information. This information will be taken and displayed back to the HTML page. See demo online:

PHP array dynamic

See online demo and code

Following are the steps to do this task:

Step 1:

Creating the HTML markup and CSS to show textbox fields along with a submit button. It also includes a button to add textboxes for more users.

 

Step 2:

The jQuery code where, I used the $.append method to add textbox fields for user name and email.

 

If you notice, the names of the textboxes are the same in markup and jQuery part:

For user name: username[]

For email: useremail[]

This will help in using these as arrays in the PHP code.

Step 3:

The CSS for the table, paragraph, and div elements. The table’s CSS class is used in the PHP section for displaying entered users’ data.

See the <style> section in the demo page.

Step 4:

This is where you need to focus at, i.e. where I used the PHP arrays:

 

First of all, user names and emails are collected, that can be any number; 1, 2, 3 even 1000+. These are assigned to PHP variables that are arrays.

In the next line, echo statement is used to create an HTML table with table headings. The table tag is assigned the CSS class that I created in the head section.

After that, a foreach loop is used to go through the arrays. The array elements (user names and emails) are printed in the HTML table.

Note that, this is again just for the demo purpose to make you understand how PHP arrays can be mixed up with web page elements.

In other scenarios, you may need to store user’s data (or some other information) in the database’s table. So rather using an HTML table in the foreach, you can write the script to insert data in a database.

An example of two-dimensional array

Creating an array inside existing array is referred as the multi-dimensional array. Alternatively, you can say, when an array acts as the value of an array it becomes a multi-dimensional array.

If you go the two levels, it will be two-dimensional array. For three levels, it will be called three-dimensional arrays.

In all above examples, the arrays were single dimensional. Because, we simply used the key/value pairs.

If key = another array, this will be two dimensional.

See the following example of creating and using the two-dimensional array.

See online demo and code

So this is how, a two dimentional array is created:

 

You may also create this as follows:

 

This is also equivalent of the above example to create two dimensional arrays:

 

In the demo, the foreach loop is used to access the array elements.

A few useful array method examples

Now let me show you a few examples of using the useful array functions. There are plenty of array PHP functions. You can see the list here.

The array count function example

The count function is used to return the total elements in an array. See the following example to use PHP count function with an array:

See online demo and code

After creating an array of five elements, a for loop is used. In the for loop, count function is used to return the length of array:

 

The loop will keep on executing until all elements of the array are displayed.

A PHP array sort example

The array sort function is used arrange the array elements from lowest to highest or alphabetically.

See a demo online where an array of string elements is created:

See online demo and code

 

In the demo, you can see the array before and after using the sort method. Initially, array was created randomly:

 

Before and after using the sort method, the output is:

 

 

You can see, array is arranged alphabetically after using the PHP sort array method.