An array in PHP is a special variable that can hold multiple values of different data types in a single variable. Learn all about PHP Arrays and their different types with simple examples:
In this tutorial, you will learn an introduction to arrays, types of PHP arrays, PHP array functions, array sorting, and frequently asked questions (FAQs).
Please note that we have used PHP version 7 in all examples.
Let’s begin!
=> Series of Simple PHP Tutorials
Table of Contents:
How to Use PHP Arrays

Introduction to PHP Arrays
An array is a type of data structure that is used to store multiple values in a single variable. Arrays are also known as special variables.
A normal variable can store only a single value, but an array can store multiple values. Thus, it requires a lesser number of code lines.
Assume that you want to store 100 values. If you use variables to store the values, you need to declare 100 variables. But you may store all the values by creating a single array (depending on the purpose).
Types of PHP Arrays
There are three types of PHP arrays. They are
- Indexed arrays
- Associative arrays
- Multidimensional arrays
Let’s discuss each of them in detail.
#1) PHP Indexed Arrays
An indexed array is an array with a numeric index. The important point to remember is that the index starts with zero, not 1.
There are two methods to create an indexed array.
Method 1: By assigning the index automatically.
Syntax:
$var = array (value1, value2, value3,...);
Alternatively, you may use the following syntax:
$var = [value1, value2, value3,...];
An example is shown below. You can practice this example by running the following programming code:
<?php
$fruits = array ("apple", "mango", "banana");
echo count($fruits);
?>
Output:
3
Method 2: By assigning the index manually.
Syntax:
$var[0] = value1; $var[1] = value2; $var[2] = value3;
An example is shown below. You can practice this example by running the following programming code:
<?php
$fruits[0] = "apple";
$fruits[1] = "mango";
$fruits[2] = "banana";
echo count($fruits);
echo "<br>";
echo $fruits[1];
?>
Output:
3 mango
#2) PHP Associative Arrays
An associative array is an array with numeric keys. There are two methods to create an associative array.
Method 1: By assigning the index automatically.
Syntax:
$var = array (key1 => value1, key2 => value2, key3 => value3,...);
Alternatively, you may use the following syntax:
$var = [key1 => value1, key2 => value2, key3 => value3,...];
An example is shown below. You can practice this example by running the following programming code:
<?php
$scores = array("David" => 1085, "Alex" => 990, "Bell" => 1001);
echo count($scores);
?>
Output:
3
Method 2: By assigning the index manually.
Syntax:
$var[key1] = value1; $var[key2] = value2; $var[key3] = value3;
An example is shown below. You can practice this example by running the following programming code:
<?php
$scores["David"] = 1085;
$scores["Alex"] = 990;
$scores["Bell"] = 1001;
echo count($scores);
echo "<br>";
echo $scores["Bell"];
?>
Output:
3 1001
#3) PHP Multidimensional Arrays
A multidimensional array contains one or more arrays.
Syntax:
$var = array
(
array(key1 => value1, key2 => value2, key3 => value3,...),
array(key1 => value1, key2 => value2, key3 => value3,...),
array(key1 => value1, key2 => value2, key3 => value3,...),
...
)
An example is shown below. You can practice this example by running the following programming code:
<?php
$marks = array
(
array("STU0025",96,32,48,75),
array("STU0026",78,100,92,61),
array("STU0027",51,49,54,68),
array("STU0028",81,67,67,70)
);
echo count($marks);
?>
Output:
4
PHP Array Functions
There are 80+ built-in array functions in PHP. Due to the space limit, we have only shown a few of them in the list below. You may visit this link to find the full detailed list.
- array() – It creates an array.
- count() – It returns the total number of elements or values in an array.
- is_array() – It checks if a variable is an array or not.
- in_array() – It checks if a given value exists in an array or not.
- sort() – It sorts an array in ascending order.
An example is shown below. You can practice this example by running the following programming code:
<?php
$colours = array('read','green','yellow','blue');
echo count($colours);
?>
Output:
4
Sorting Arrays
We have already discussed array functions. Some of the array functions are used to sort arrays.
The order of sorting can be alphabetical, ascending (low to high), descending (high to low), natural, random, or user-defined.
The following list shows array sorting functions:
- sort() – Sorts by value in ascending order.
- rsort() – Sorts by value in descending order.
- asort() – Sorts by value in ascending order.
- arsort() – Sorts by value in descending order.
- ksort() – Sorts by key in ascending order.
- krsort() – Sorts by key in descending order.
- natcasesort() – Sorts by value in natural order.
- natsort() – Sorts by value in natural order.
- usort() – Sorts by value in user-defined order.
- uasort() – Sorts by value in user-defined order.
- uksort() – Sorts by key in user-defined order.
- shuffle() – Sorts by value in random order.
- array_multisort() – Sorts by value.
An example is shown below. You can practice this example by running the following programming code:
<?php
$values = [8,11,25,7,9,58,3];
rsort($values);
print_r($values);
?>
Output:
Array ( [0] => 58 [1] => 25 [2] => 11 [3] => 9 [4] => 8 [5] => 7 [6] => 3 )
Frequently Asked Questions
1. What are PHP arrays?
PHP arrays are special variables for storing multiple values in a single variable.
2. What are the different types of arrays in PHP?
There are 3 types of arrays in PHP. They are named indexed, associative, and multidimensional arrays.
3. What is an associative array in PHP?
They are the arrays that contain name keys.
4. How do you get the PHP array length?
The PHP count() function can be used to get the number of elements in an array.
5. What is a PHP array function?
The array() function is an inbuilt function to create an array.
6. What is the use of the is_array () function?
The is_array() function checks if a variable is an array or not, and returns true if the variable is an array. Otherwise, it returns false (nothing).
7. What is the PHP in_array() function?
The PHP in_array() function checks if a given value exists in an array or not. It returns true if the given value is found. Otherwise, it returns false (nothing).
Conclusion
An array is a type of data structure that is used to store multiple values in a single variable.
There are 3 types of arrays in PHP called indexed, associative, and multidimensional arrays. PHP has several built-in functions to deal with arrays for different purposes, such as array sorting etc.






