Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Arrays in PHP are a type of data structure, which allows us to saves the efforts of creating a

different variable in order to store multiple elements with a similar data type under a single
variable. The arrays help to create a list of similar elements, accessible by index or key. The array
is used here to store each element in one variable and also to enable easy access via an index or
a key. Using a function in PHP, an array is generated. Now we will try to understand arrays in PHP
using the syntax and examples. PHP arrays are implemented by the use of array function which
is written as “array()”. It has a very simple syntax that makes the usage of the array very easy.
Below is the syntax to declare and initialize the array in PHP.
array_name = array("value1", "value2", "value3",..)

Types of PHP Array


In PHP we have two types of array: one-dimensional array and multi-dimensional array.
1. One-dimensional array
● The one-dimensional array may be defined as the type of array that consists of multiple
values assigned to the single array.
● The Values assigned in the one-dimensional array are homogenous in nature that means it
allows only the values with the same data type to be added to the array.
● Syntax will be like. $array_name = array("value1", "value2", "value3")

2. Multi-dimensional array
● A multi-dimensional array may be defined as the kind of array that allows us to assign
values in multiple arrays at the same time.
● The values assigned to the multi-dimensional array could be heterogeneous that means
values of different data types could be stored in the arrays and it has to be taken care that
the sequence of items of specific data types should be same in all the arrays.
● Here we will look on two-dimensional array syntax. Well, just for your information, the
complexity of array increases as the dimension of the array increases.
$array_name = array
(
array("String1", "Int1", "Int11"),
array("Stirng2", "Int2", "int22"),
array("String3", "Int3", "int33"),
array("String4", "Int4", "int44"),
)

Advantages
1. Multiple values under one name
2. Endorse data structure implementation
5. Less memory utilization
Easy to rember

You might also like