PHP Arrays

PHP Array

PHP Array

An array can store one or more values in a single variable name.

What is an Array?

When working with PHP, sooner or later, you might want to create many similar variables to save the values.

Instead of having many similar variables, you can store the data as elements in an array

Each element in the array has its own ID so that it can be easily accessed.

There are three types of array in PHP.

Array Types:

In PHP, we have three different types of array available.

  1. Numeric Array: An PHP array with a number ID key
  2. Associative Array- An PHP array where each ID key is associated with a value.
  3. Multidimensional Array – An PHP array containing one or more arrays.

Numeric Array

A numeric array stores each element with a numeric ID key. By default array index starts from zero. There are different ways to create a numeric array.

Example 1

In above example the ID key is automatically assigned.

Example 2

In above example we assign the ID manually.

You can use the ID keys to print the value like this.

The code output will be:

Chrome, Firefox, Opera, IE are browser

Associative Arrays

An associative array, each ID key is associated with a value. When storing data about specific named values, a numerical array is not always the best way to do it. With associative arrays we can use the values as keys and assign values to them.

Example 1

In above example we use an array to assign values to different browsers.

Example 2

The code output is:

Chrome and Firefox are my favorite browser.

Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.


Output will be:


Previous articlephp email
Next articlePHP Tutorials


Please enter your comment!
Please enter your name here