Creating arrays
Using array() construct
Syntax:
<?php
$empty_array = array();
Ex:
1 2 3 | <?php $scores = array(1, 2, 3); |
Using the [] syntax
PHP provides a more convenient way to define arrays with the shorter syntax [], known as JSON notation.
Syntax:
<?php
$empty_array = [];
Ex:
1 2 3 | <?php $scores = [1, 2, 3]; |
Displaying arrays
To show the contents of an array, you use the var_dump() function.
Ex:
1 2 3 4 | <?php $scores = [1, 2, 3]; var_dump($scores); |
OutputL:
1 2 3 4 5 | array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } |
Or you can use the print_r() function:
1 2 3 4 | <?php $scores = array(1, 2, 3); print_r($scores); |
Output:
1 2 3 4 5 6 | Array ( [0] => 1 [1] => 2 [2] => 3 ) |
Accessing array elements
To access an element in an array, you specify the index of the element within the square brackets:
1 2 3 4 5 6 | //The following example shows how to access the first element of the array: <?php $scores = [1, 2, 3]; echo $scores[0]; //1 |
Adding an element to the array
Syntax:
PHP will calculate the highest numerical index plus one each time you assign an element to the array.
Ex:
1 2 3 4 5 6 | // The following example shows how to add the number 4 to the $scores array: <?php $scores = [1, 2, 3]; $scores[] = 4; |
In this example, we defined an array that consists of three numbers initially. Then, we added the number 4 to the array.
It’s possible to use an index when you add a new element to the array.
1 2 | $scores = [1, 2, 3]; $scores[3] = 4; |
Changing array elements
The following statement changes the element located at the index to the $new_element:
Ex:
1 2 3 4 5 6 | // For example, to change the first element of the $scores array from 1 to zero <?php $scores = [1, 2, 3]; $scores[0] = 0; |
Removing array elements
To remove an element from an array, you use the unset() function. The following removes the second element of the $scores array:
1 2 3 4 | <?php $scores = [1, 2, 3]; unset($scores[1]); |
Getting the size of an array
To get the number of elements in an array, you use the count() function.
Ex:
1 2 3 4 5 | <?php $scores = [1, 2, 3, 4, 5]; echo count($scores); |
Output:
1 | 5 |
Sorting Arrays
Sort Array in Ascending Order – sort()
Ex:
1 2 3 4 5 6 7 8 9 10 | <?php $numbers = array(40, 61, 2, 22, 13); sort($numbers); $arrlength = count($numbers); for($x = 0; $x < $arrlength; $x++) { echo $numbers[$x]; echo "<br>"; } ?> |
Output:
1 2 3 4 5 | 2 13 22 40 61 |
Sort Array in Descending Order – rsort()
Ex:
1 2 3 4 5 6 7 8 9 10 | <?php $numbers = array(40, 61, 2, 22, 13); rsort($numbers); $arrlength = count($numbers); for($x = 0; $x < $arrlength; $x++) { echo $numbers[$x]; echo "<br>"; } ?> |
Output:
1 2 3 4 5 | 61 40 22 13 2 |
Sort Array in Ascending Order, According to Value – asort()
Ex:
1 2 3 4 5 6 7 8 9 | <?php $age = array("ayush"=>"23", "shankar"=>"47", "kailash"=>"41"); asort($age); foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; } ?> |
Output:
1 2 3 | Key=Ayush, Value=23 Key=Kailash, Value=41 Key=Shankar, Value=47 |
Sort Array in Ascending Order, According to Key – ksort()
Ex:
1 2 3 4 5 6 7 8 9 | <?php $age = array("ayush"=>"23", "shankar"=>"47", "kailash"=>"41"); ksort($age); foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; } ?> |
Output:
1 2 3 | Key=Ayush, Value=23 Key=Kailash, Value=41 Key=Shankar, Value=47 |
Sort Array in Descending Order, According to Value – arsort()
Ex:
1 2 3 4 5 6 7 8 9 | <?php $age = array("ayush"=>"23", "shankar"=>"47", "kailash"=>"41"); arsort($age); foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; } ?> |
Output:
1 2 3 | Key=Shankar, Value=47 Key=Kailash, Value=41 Key=Ayush, Value=23 |
Sort Array in Descending Order, According to Key – krsort()
Ex:
1 2 3 4 5 6 7 8 9 | <?php $age = array("ayush"=>"23", "shankar"=>"47", "kailash"=>"41"); krsort($age); foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; } ?> |
Output:
1 2 3 | Key=Shankar, Value=47 Key=Kailash, Value=41 Key=Ayush, Value=23 |