In this PHP tutorial, you will learn all about Arrays in PHP. We will discuss in detail the Array of various types of arrays such as Indexed array, Associative array, Multidimensional array and we also discuss the Array Functions.
Arrays are one of the data structures in PHP. An array is used to store the same type of data in a contiguous memory location. We can store any amount of data in the array. The “array ()” function is used to create an array in PHP. There are mainly three types of arrays namely Indexed Array, Associative Array, Multidimensional Array
Indexed arrays are those arrays whose every element has an index value that begins with 0. By default, every array in PHP is an indexed array. An indexed array in PHP can be used to store integers, strings, or any object. An indexed array in PHP is also known as a numeric array.
There are mainly two ways to create an indexed array in PHP
$languages = array ("PHP", "Python", "GO", "JavaScript");
foreach ($languages as $language) {
echo "$language \n";
}
Output:
PHP Python GO JavaScript
In the above example we can see that we have created the array using the function “array ()”. By using the array () function we can create an array by passing the required elements as the arguments and it will automatically be indexed at each position. And we have also printed every element of the array using the foreach loop.
$languages[0] = "PHP";
$languages[1] = "Python";
$languages[2] = "GO";
$languages[3] = "JavaScript";
foreach ($languages as $language) {
echo "$language \n";
}
Output:
PHP Python GO JavaScript
In the above example, we can see that we have created the array by assigning the element to the appropriate index of the array manually. It is not the recommended manner to do so as it is more time-consuming and it also increases the code. And we have also printed every element of the array using the foreach loop.
Indexed arrays are those arrays where every element in the array is stored with a name or key instead of the index. In PHP key-value pairs are created by using the “=>” symbol. In PHP, because each element is represented by a label rather than an incremented number, you can easily remember it.
$ages = array ("Jhon" => 23, "Roy" => 15, "Justin" => 20, "Bob" =>27);
foreach ($ages as $name=>$age) {
echo "$name is $age years old \n";
}
Output:
Jhon is 23 years old Roy is 15 years old Justin is 20 years old Bob is 27 years old
In the above example, we can see that we have created the array using the function “array()”. By using the array() function we can create an array bypassing the required elements with its corresponding key as the arguments. And we have also printed every element of the array using the foreach loop.
$ages["Jhon"] = 23;
$ages["Roy"] = 15;
$ages["Justin"] = 20;
$ages["Bob"] = 27;
foreach ($ages as $name=>$age) {
echo "$name is $age years old \n";
}
Output:
Jhon is 23 years old Roy is 15 years old Justin is 20 years old Bob is 27 years old
In the above example, we can see that we have created the array by assigning the element to the appropriate key of the array manually. It is not the recommended manner to do so as it is more time-consuming and it also increases the code. And we have also printed every element of the array using the foreach loop.
In PHP the multidimensional array is used to store the arrays in the array itself. It helps to store the data in tabular format. PHP multidimensional arrays may be expressed as matrices, which are represented as row * column.
$students = array
(
array(1,"Bob",20),
array(2,"John",21),
array(3,"Roy",20)
);
for ($row = 0; $row < 3; $row++) {
for ($col = 0; $col < 3; $col++) {
echo $students[$row] [$col]." ";
}
echo "\n";
}
Output:
1 Bob 20 2 John 21 3 Roy 20
In the above example, we can see that to create a multidimensional array we have to create multiple arrays inside an array. By this, we can store the elements in the matrix form. And we have also printed every element of the array.
PHP has a number of array methods for accessing and manipulating the elements in the array. The most used array functions are mentioned below.
array()
function is used to create and return an array. It is used to create indexed, associative, and multidimensional arrays. Syntax :
array(value1, value2, value3, ...);
$languages = array ("PHP", "Python", "GO", "JavaScript");
echo $languages[2];
Output:
GO
array_change_key_case()
function is used to change the case of the key of the element in the array. Syntax :
array_change_key_case(array, case);
$ages = array ("Jhon" => 23, "Roy" => 15, "Justin" => 20, "Bob" =>27);
print_r(array_change_key_case($ages,CASE_UPPER));
Output:
Array ( [JHON] => 23 [ROY] => 15 [JUSTIN] => 20 [BOB] => 27 )
array_chunk()
function is used to split the array into several parts. Syntax :
array_chunk(array, size, preserve_key);
$ages = array ("Jhon" => 23, "Roy" => 15, "Justin" => 20, "Bob" =>27);
print_r(array_chunk($ages,2));
Output:
Array Array ( [0] => Array ( [0] => 23 [1] => 15 ) [1] => Array ( [0] => 20 [1] => 27 ) )
count()
function is used to return the number of elements in the array. Syntax :
count(array, mode(optional));
$languages = array ("PHP", "Python", "GO", "JavaScript");
echo count($languages);
Output:
4
list()
function is used to assign the elements in the array to the variables in a single operation. Syntax :
list(var1, var2, ...)
$languages = array ("PHP", "Python", "GO", "JavaScript");
list($a, $b, $c, $d) = $languages;
echo $a. "\n";
echo $b. "\n";
echo $c. "\n";
echo $d. "\n";
Output:
PHP Python GO JavaScript
sort()
function is used to sort the array in order Syntax :
sort(array, sorttype (optional));
$languages = array ("PHP", "Python", "GO", "JavaScript");
sort($languages);
print_r($languages);
Output:
Array ( [0] => GO [1] => JavaScript [2] => PHP [3] => Python )
array_reverse ()
function is used to arrange the array in reverse order. Syntax :
array_reverse(array, preserve);
$languages = array ("PHP", "Python", "GO", "JavaScript");
print_r(array_reverse($languages));
Output:
Array ( [0] => JavaScript [1] => GO [2] => Python [3] => PHP )
array_search()
function is used to search the element in the array and return its index or key if the value exists in the array. Syntax :
array_search(value, array, strict(optional));
$languages = array ("PHP", "Python", "GO", "JavaScript");
echo array_search("GO", $languages);
Output:
2
array_merge()
function is used to merge two or more arrays together. Syntax :
array_merge(array1, array2, array3, ...);
$languages1 = array ("GO", "JavaScript");
$languages2 = array ("PHP", "Python");
print_r(array_merge($languages1, $languages2));
Output:
Array ( [0] => GO [1] => JavaScript [2] => PHP [3] => Python )
array_intersect ()
function is used to compare two or more array and return the common elements. Syntax :
array_intersect(array1, array2, array3, ...);
$languages1 = array ("GO", "JavaScript", "R");
$languages2 = array ("PHP", "Python", "GO", "R");
print_r(array_intersect($languages1, $languages2));
Output:
Array ( [0] => GO [2] => R )