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.

You can also search for these topics, php array, array php variable, php array types, array php values, php array index value, php array example.

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 :-

<?php
$colors = array("Red", "Green", "Blue");
echo "I like " . $colors[0] . ", " . $colors[1] . " and " . $colors[2] . ".";
?>

Output :-

I like Red, Green and Blue.

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.
You can also search for these topics, create an array in php, create an array example using php, php array() function, types of arrays in php, php create array with value, php array() function example.

Get The Length of an Array - The count() Function

The length (number of entries) of an array is returned by the count() method.

Example :-

<?php
$colors = array("Red", "Green", "Blue");
echo "Total colors :- " . count($colors);
?>

Output :-

Total colors :- 3

You can also search for these topics, php get the length of an array, php get the length of a string, php count array length, php count array values, php count string, php count() function example.

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";
You can also search for these topics, php indexed array, how many ways to create indexed array, php indexed array example, php through an indexed array, php indexed array begin with position, php indexed array to string.

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 :-

<?php
$cars = array("Volvo", "BMW", "Toyota");
$length = count($cars);

for($x = 0; $x < $length; $x++) {
  echo $cars[$x];
  echo "<br />";
}
?>

Output :-

Volvo
BMW
Toyota

You can also search for these topics, how to loop through an indexed array in php, php loop through an indexed array example, php loop through an indexed array of object, php loop through indexed array length.

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 :

<?php
$age = array("Peter"=>"35", "Joe"=>"43");

echo "Peter is " . $age['Peter'] . " years old.";
echo "Joe is " . $age['Joe'] . " years old.";
?>

Output :-

Peter is 35 years old.
Joe is 43 years old.

You can also search for these topics, Associative array in php, Example of associative array in php, how many ways to create the associative arrays, php associative array to string, php associative array keys.

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 :-

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $key_name => $key_value) {
  echo "Key = " . $xkey_name . ", Value = " . $key_value;
  echo "<br />";
}
?>

Output :-

Key = Peter, Value = 35
Key = Ben, Value = 37
Key = Joe, Value = 43

You can also search for these topics, how to loop through an associative array in php, php loop through an associative array example, php loop through an associative array of object, php loop through associative array key value.

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.
You can also search for these topics, multidimentional arrays in php, define multidimentional array in php, php multidimentional array example, php multidimentional array to single array, php multidimentional array with keys, php multidimentional array sort by value.

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 :

<?php
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br />";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br />";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br />";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br />";
?>

Output :-

Volvo: In stock: 22, sold: 18.
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):

<?php
for ($row = 0; $row < 4; $row++) {
  echo "<p><b>Row number $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 3; $col++) {
    echo "<li>".$cars[$row][$col]."</li>";
  }
  echo "</ul>";
}
?>

Output :-

Row number 0
Volvo
22
18

Row number 1
BMW
15
13

Row number 2
Saab
5
2

Row number 3
Land Rover
17
15


You can also search for these topics, Two dimentional arrays in php, define Two dimentional array in php, php Two dimentional array example, php two dimentional array declaration, php two dimentional array search, php two dimentional array getvalue, php two dimentional array loop inside another loop.

PHP Sorting Arrays

The components in a table can be sorted by descending or ascending, in alphabetical or numerical format.

You can also search for these topics, sorting an array in php, sorting functions for arrays in php, list the sorting array functions, php sorting array function practice, php sorting array by value.

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.
You can also search for these topics, php sorting array function, types of sort function in php, array sorting function using php.

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 :

<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>

Output :-

BMW
Toyota
Volvo

Example 2 :- This example arranges $numbers in ascending numerical order : The following items are :

<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
?>

Output :-

2
4
6
11
22

You can also search for these topics, php sorting array in ascending order, php sort() function, php sort array in ascending order example, php array sort ascending order, what is the use of sort() function?.

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 :

<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>

Output :-

Volvo
Toyota
BMW

Example 2 :- The items of the $numbers array are sorted in decreasing numerical order in the example below :

<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>

Output :-

22
11
6
4
2

you can also search for these topics, php sorting array in descending order, php rsort() function, php rsort array in descending order example, php array sort descending order, what is the use of rsort() function?.

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 :

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>

Output :-

Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43

You can also search for these topics, php sort array according to value, php asort() function, php asort() function example, according to value sort the array using php, php sort array according to value ascending order.

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 :

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>

Output :-

Key=Ben, Value=37
Key=Joe, Value=43
Key=Peter, Value=35

You can also search for these topics, php sort array according to key ascending order, php ksort() function, php ksort() function example, php sort array according to key ascending order use ksort() function.

Sort Array (Descending Order), According to Value - arsort()

Example :- In descending order, according to value, the following example sorts an associated array:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
arsort($age);
?>

Output :-

Key=Joe, Value=43
Key=Ben, Value=37
Key=Peter, Value=35

You can also search for these topics, php sort array according to value descending order, php arsort() function, php arsort() function example, php sort array according to value descending order use arsort() function.

Sort Array (Descending Order), According to Key - krsort()

Example :- The example below sorts an associative array in decreasing order based on the key :

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>

Output :-

Key=Peter, Value=35
Key=Joe, Value=43
Key=Ben, Value=37