PHP Array

In this lesson, you will learn about arrays in PHP, numeric arrays, and their usage, along with examples to better understand the topics.


PHP array

An array is a variable that can retain more than one value at the moment. An array in PHP is an ordered map, a way of associating values with keys. It can be used as an array, list (vector), dictionary, collection, stack, queue, and hash table (a map implementation).

As array values can be other arrays, using trees and multidimensional arrays is also feasible. If you have a list of products (for instance, a list of fruit names), it might look like this to store the fruits in single variables:

$Fruit1 = "Apple";
$Fruit2 = "Banana";
$Fruit3 = "Mango";

But what if you want to browse all the fruits and find one in particular? What if you had 300 fruits instead of just three? The remedy is to use an array! Arrays can store multiple values under a single variable, and you can access the values by using an index number.

Why do we need an array?

  • Less Code: It’s not necessary to define multiple variables.
  • Simple to traverse: With the help of a single loop, we can quickly go through every element of an array.
  • Sorting: The elements of the array can be sorted.

Create an array

You can create an array using the language structure of the array(). Any number of value pairs separated by commas must be provided as arguments. The comma is optional and can be omitted after the last array element.


PHP Numeric array

The PHP index is the number starting from the zero position. The PHP array supports the storage of numbers, strings, and objects. All components of the PHP set are allocated by default to an index number. An indexed or numeric array stores a numeric index for each array element. Numeric arrays use the number as access keys, and an access key in an array variable is a reference to a memory slot. The access key is utilized when an array element is read or given a new value. Below is the PHP numeric array syntax.

There are two methods to declare an array of numbers:

Syntax of array

1st Method:

$nameoffruits=array("value1"," value2"," value3"," value4"); // generic
$nameoffruits=array("apple","banana","orange","pineapple");

2nd Method:

$ nameoffruits [n]=" apple";
$ nameoffruits [n+1]=" banana";
$ nameoffruits [n+2]=" orange";
$ nameoffruits [n+3]=" pineapple";
“$nameoffruits” is the name of the variable
“[n]” is the access index number of the member
"value" is the value allocated to the array member.

Example

<?php
echo "PHP ***Numeric Array*** Example";
echo "<br>";
echo " ------------Ist method ------------------ ";
echo "<br>";
$luxuryCars = array(
    "Volvo",
    "Audi,",
    "Ferrari"
);
echo "I would like to have " . $luxuryCars[0] . ", " . $luxuryCars[1] . " and " . $luxuryCars[2] . ".";
echo "<br>";
echo "<br>";
echo " ------------2nd method ------------------ ";
echo "<br>";
$luxuryCars[0] = "Volvo";
$luxuryCars[1] = "Audi,";
$luxuryCars[2] = "Ferrari";
echo "I would like to have " . $luxuryCars[0] . ", " . $luxuryCars[1] . " and " . $luxuryCars[2] . ".";
?>

Output

PHP ***Numeric Array*** Example
------------Ist method ------------------
I would like to have Volvo, Audi, and Ferrari.

------------2nd method ------------------
I would like to have Volvo, Audi, and Ferrari

PHP Traversing Numeric Array

Using the foreach loop, we can readily cross an array in PHP. Let’s see a simple example to go through all the PHP array components.

Example

echo "PHP ***Numeric Array*** Example";
echo "<br>";
echo " ------------Traversing PHP Numeric Array ------------------ ";
echo "<br>";
$luxuryCars = array( "Volvo", "Audi", "Ferrari" );
foreach ( $luxuryCars as $lc ) {
echo "luxuryCars are: $lc<br />";
}
?>

Output

PHP ***Numeric Array*** Example
------------Traversing PHP Numeric Array ------------------
luxuryCars are: Volvo
luxuryCars are: Audi
luxuryCars are: Ferrari

PHP Count function Numeric Array

PHP offers the count() function to give the length of an array.

Example

<?php
echo "PHP ***Numeric Array*** Example";
echo "<br>";
echo " ------------ PHP Count Function over Numeric Array ------------------ ";
echo "<br>";
$luxuryCars = array(
    "Volvo",
    "Audi",
    "Ferrari"
);
echo count($luxuryCars);
?>

Output

PHP ***Numeric Array*** Example
------------ PHP Count Function over Numeric Array ------------------
3

This concludes the PHP Array lesson. In the next lesson, you will learn about Array Functions.