PHP Arrays
An array
is a single variable that holds numerous values of same datatype.
An array
is a type of variable that may hold several values at the same time.
If you have a list of objects
(for example, a list of vehicle names), it may look like this when saving vehicles into individual variables.
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
But what if you want to search through all of the automobiles to find a specific one? What if you had 300 cars instead of three?
To solve the problem, make an array
!
An array may have numerous values in one name, and by reference to an index number, you may retrieve the data.
Create an Array in PHP
The array()
function in PHP is used to generate an array.
array(list_of_values);
Note:- Each value seperated by comma (,) in the list.
Example :-
Output :-
In PHP, there are three types of arrays:
- Indexed arrays - Arrays that have a number index.
- Associative arrays - Arrays containing names for the keys.
- Multidimensional arrays - Arrays that are made up of one or more arrays.
Get The Length of an Array - The count() Function
The length (number of entries) of an array
is returned by the count()
method.
Example :-
Output :-
PHP Indexed Arrays
There are two ways to create indexed arrays:
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually :
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
Loop Through an Indexed Array
A for
loop, such as this one, might be used to cycle through and output all the values of an indexed array.
Example :-
";
}
?>
Output :-
BMW
Toyota
PHP Associative Arrays
Associative arrays are those that employ named keys that you provide.
Unlike an indexed array, it will not support index number of the element to access the array element.
There are two ways to create an associative array :
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
or :-
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
Example :- The named keys can then be used in a script :
"35", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
echo "Joe is " . $age['Joe'] . " years old.";
?>
Output :-
Joe is 43 years old.
Related Links
Loop Through an Associative Array
You might use a foreach
loop, like this, to cycle over and output all of the values in an associative array.
Example :-
"35", "Ben"=>"37", "Joe"=>"43");
foreach($age as $key_name => $key_value) {
echo "Key = " . $xkey_name . ", Value = " . $key_value;
echo "
";
}
?>
Output :-
Key = Ben, Value = 37
Key = Joe, Value = 43
PHP Multidimensional Arrays
You may want to save values with several keys on occasion. We use multidimensional arrays for this.
An array
comprising one or more arrays is called a multidimensional array.
PHP supports two, three, four, five, and deeper multidimensional arrays. Arrays
deeper than three levels for most individuals never the less are difficult to handle.
One array's dimension shows how many indices are required to pick an element.
- To choose an element from a two-dimensional array, you'll need two indices.
- To choose an element from a three-dimensional array, you'll need three indices.
PHP - Two-dimensional Arrays
An array
of arrays makes up a two-dimensional array (An array
of arrays makes up a three-dimensional array)
before that, just look at the below table :
Name | Stock | Sold |
---|---|---|
Volvo | 22 | 18 |
BMW | 15 | 13 |
Saab | 5 | 2 |
Land Rower | 17 | 15 |
The data from the previous table may be stored in a two-dimensional array, as seen below.
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
There are now four arrays in the $cars double-dimensional array with two indications : row and column.
Example 1 :-
We have to point to two indices (row and column) to obtain access to $cars elements :
";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".
";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".
";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".
";
?>
Output :-
BMW: In stock: 15, sold: 13.
Saab: In stock: 5, sold: 2.
Land Rover: In stock: 17, sold: 15.
Example 2 :-
In another for
a loop we can additionally place one for
loop to obtain the $cars array items (we still have two indexes to point out):
Row number $row";
echo " ";
for ($col = 0; $col < 3; $col++) {
echo "- ".$cars[$row][$col]."
";
}
echo "
";
}
?>
Output :-
Volvo
22
18
Row number 1
BMW
15
13
Row number 2
Saab
5
2
Row number 3
Land Rover
17
15
PHP Sorting Arrays
The components in a table
can be sorted by descending or ascending, in alphabetical or numerical format.
PHP - Sort Functions For Arrays
We'll go over the PHParray
sort functions in this chapter :
sort()
- Ascending order of sort arrays.rsort()
- Descending order of sort arrays.asort()
- Associative arrays should be sorted in ascending order by value..ksort()
- Associative arrays should be stored in ascending order by key..arsort()
- Associative arrays should be stored in descending order by value.krsort()
- Associative arrays should be stored in descending order order by key.
Related Links
Sort Array in Ascending Order - sort()
The sort()
function is used to sorting in ascending order of an given indexed array.
Example 1 :- The items of the $cars array are sorted in ascending alphabetical order in the example below :
Output :-
Toyota
Volvo
Example 2 :- This example arranges $numbers in ascending numerical order : The following items are :
Output :-
4
6
11
22
Sort Array in Descending Order - rsort()
The rsort()
function is used to sorting in descending order of an given indexed array.
Example 1 :- In descending alphabetical order, this example sorts the members of the $cars array :
Output :-
Toyota
BMW
Example 2 :- The items of the $numbers array are sorted in decreasing numerical order in the example below :
Output :-
11
6
4
2
Sort Array (Ascending Order), According to Value - asort()
The asort()
function is used to sort an associative array in ascending order based on array value.
Example :- In ascending order, the following example sorts a group array
according to the value :
"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>
Output :-
Key=Ben, Value=37
Key=Joe, Value=43
Sort Array (Ascending Order), According to Key - ksort()
The ksort()
function is used to sort an associative array in ascending order based on array keys.
Example :-
The example below specifies an associative array in ascending order based on the key :
"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>
Output :-
Key=Joe, Value=43
Key=Peter, Value=35
Sort Array (Descending Order), According to Value - arsort()
Example :- In descending order, according to value, the following example sorts an associated array:
"35", "Ben"=>"37", "Joe"=>"43");
arsort($age);
?>
Output :-
Key=Ben, Value=37
Key=Peter, Value=35
Sort Array (Descending Order), According to Key - krsort()
Example :- The example below sorts an associative array in decreasing order based on the key :
"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>
Output :-
Key=Joe, Value=43
Key=Ben, Value=37