Fundamentals of PHP Arrays

by Ahmed Khan  October 29, 2015

Here is another tutorial from our beginner series of PHP. In this article, we will cover the core concepts of PHP arrays, methods to create arrays in PHP, types of arrays, and the loops through arrays.

php arrays

PHP arrays are used to store more than one value in a single variable. For example, if you have a list of books you can store their names in a single variable. I am going to share few examples to make it clear.

Example:

Output:

By the use of array variables, you can skip the lines of code where you traditionally define every book name in a separate variable. A disadvantage of this approach is that you will not be able to loop through the books and sort any specific one. Hence, the best solution is to create an array.

You can access values of an array by simply referring to an index number.

How To Create An Array In PHP

You can create an array in PHP by simply using the function array().

Types of Arrays in PHP

There are three major types of arrays in PHP:

  • Indexed Arrays: Those arrays having numeric index are known as indexed arrays.
  • Associative Arrays: Those arrays having named keys are known as associative arrays.
  • Multidimensional Arrays: The array that contains more than one array are called multidimensional arrays.

Creating PHP indexed arrays

There are two methods of creating indexed arrays:

Method 1

Method 2

Example

Consider the following example in which an array $colors is created, assigned three elements and then printed a text containing array values.

Output

Function for Getting the Length of an Array

Count() function returns the length of an array.

Example

Output

Loop Through an Indexed Array

We use for loop to loop through and print all values of an indexed array.

Example

Output

PHP Associative Arrays

There are two ways to create associative arrays. These are the arrays which use named keys assigned by the user. There are two methods to create an associative array:

 Method 1

Method 2

Output

Loop through an Associative Array

To loop values within an associative array, foreach loop is used.

Consider the following example.

Example

Output

Till now, we have discussed those arrays that contain single value pairs. But in complex programming, a time comes when you need more than one key. That is where the concept of multidimensional arrays comes into play.

Multidimensional Arrays

An array containing one or more than one arrays are called multidimensional arrays. PHP supports multidimensional arrays that are of more than 2 levels. However, 3 or more levels of array are hard to manage.

Dimension basically implies the number of indices needed for selecting an element.

PHP 2D Arrays

They are basically an array of arrays. To better understand the concept, consider the following example:

We can store the data from the table above in a two-dimensional array, like this:

Output

Above example contains 2D array $colors which contains four values. It contains two indices, row and column. Hence by pointing two indices you can simply access the $colors array elements.

For Loop for 2D array

A for loop within a for loopcan be used to access the elements of 2D array. Let’s consider the following example:

Example

Output

This finishes our tutorial for PHP arrays. They are mostly used in complex programming. Keep following this blog for more tutorials on the Fundamentals of PHP.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...