How to Declare an Array in PHP With Examples and Explanation

Last Updated on January 1, 2021 by Roshan Parihar

In this tutorial, learn how to declare an array in PHP with examples. The short answer is: use PHP array() to create an array in PHP. An array is one of the best options in PHP to store data in a single variable. Below is a simple example of an array in PHP.

Let’s find out more about array and the examples to create an array in PHP.

What is an Array in PHP?

Array in PHP is the way of storing multiple data of similar data types in a single variable. It saves the spaces by locating all the data in the same variable and each data are of a similar type.

Each data stored in its index position and keys and data can be easily accessed with their index and keys. The array makes it easier in the data structure to store data.

Let’s take an example that if you have three items to store in three different variables, you can store them individually as given below.

But, if you have hundreds of data, you cannot easily create variables for them or accessed each one of them individually. The array makes it easier by arranging all the data in a single variable in which data can be easily accessed by using index and keys.

Let’s find out how to create different types of arrays and the methods to access data in PHP.

How to Declare Array in PHP?

You can easily declare an array in PHP by using the PHP array(). There are three types of an array in PHP that are given below.

  1. Indexed or Numeric Arrays: Array that stores data in a variable with numeric index values.
  2. Associative Arrays: Array that stores data in a variable with a string index.
  3. Multidimensional Arrays: Array that contains one or more than one arrays in a single variable.

1. Indexed or Numeric Arrays

It is an array in which elements are stored in a variable with a numeric index value. The elements are automatically assigned an index value that starts from 0 for the first element.

There are two ways of creating an indexed array in PHP. You can choose which one you prefer to use in PHP.

The first way of creating an indexed array in which the index is automatically assigned to the elements.

Output

Cycle
Bike
Car
Bolero

The second method of creating an indexed array in which index values are manually assigned.

Output

Cycle
Bike
Car
Bolero

Traversing Through Indexed Array Elements Using Foreach Loop

Traversing each element of an indexed array is the process of accessing elements individually. You can use PHP Foreach loop or PHP for loop to get each element one-by-one. See the examples to learn the method of traversing.

Using a Foreach loop in PHP:-

Output

Cycle
Bike
Car
Bolero

Using For loop in PHP:-

Output

Cycle
Bike
Car
Bolero

2. Associative Arrays

Associative arrays contain the elements in which each element has an assigned key of string type. The elements are manually assigned index keys that are user-defined.

There are two ways of creating an associative array in PHP. You can choose which one you prefer to use to declare an array in PHP.

The first way of creating associative arrays in which keys are assigned within array().

Output

2
5
9

The second method of creating associative arrays in which keys and values are manually assigned.

Output

2
5
9

Traversing Through Associative Array Elements Using Foreach Loop

Traversing through each element of an associative array is the process of access its keys and its relevant values individually. You can use the Foreach loop in PHP and For loop of PHP to get a sequence of keys with their relevant values one-by-one.

Using the Foreach loop in PHP:-

Output

Key is: Cycle, Value is: 2
Key is: Bike, Value is: 5
Key is: Car, Value is: 9

Using For loop in PHP:-

Output

Key is: Cycle, Value is: 2
Key is: Bike, Value is: 5
Key is: Car, Value is: 9

3. Multidimensional Arrays

Multidimensional arrays is an array that contains another array in each index and called an array of array. It has certain level of array and sub-array that can be accessed using multiple indices.

There are two, three, four, and more dimensions in a multidimensional array. The more the number of dimensions in a multidimensional array, the more is the indices and more difficult to access elements.

In the below example, you will learn a two-dimensional array that can be accessed with two indices. It contains an associative array inside an array. However, you can also create a two-dimensional array using the indexed array inside the array in the same manner.

Creating a multidimensional array using associative arrays in which keys are manually assigned within array().

Output

First data contains 2 Cycle, 5 Bike, and 9 Car.
Second data contains 3 Cycle, 6 Bike, and 10 Car.
Third data contains 5 Cycle, 8 Bike, and 12 Car.

Traversing Through Multidimensional Array Elements Using Foreach Loop

Traversing over a multidimensional array is the process of accessing each inner array and the data inside them. After accessing the array, it accesses the keys and their relevant values. The multidimensional array can contain an indexed array or associative array as the elements of it.

For two dimensional array, you have to use the nested loop to access the elements. It can be for loop with Foreach loop or for loop inside to get the required data.

Using the Foreach loop in PHP:-

Output

Row 0 contains below data
Cycle:2
Bike:5
Car:9

Row 1 contains below data
Cycle:3
Bike:6
Car:10

Row 2 contains below data
Cycle:5
Bike:8
Car:12

You May Also Like to Read

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.