PHP Arrays


The array is a special type of variable that can contain various variables.

04.01.2023 | Ilir Raufi | Blog, Development

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses, it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.An array is the collection of similar data items.

Syntax of Array

array(“value1”, “value2 “, “value3”);

In the above syntax of an array, we have declared an array() function with multiples values.In above syntax , you can see an array() function contained value1 , value 2 and value 3.

Create an Array in PHP

In PHP, the array() function is used to create an array and there are three types of arrays:

  • Indexed arrays – Arrays with a numeric index.
  • Associative arrays – Arrays with named keys.
  • Multidimensional arrays – Arrays containing one or more arrays.

PHP Index Array

The index array is the array in which values of the array being assigned to an index number. As the name(index array) indicates, the index array has importance of the index numbers .

There are two ways to create indexed arrays:

The index can be assigned automatically (index always starts at 0), like this:

$cars = array(“Volvo”, “BMW”, “Toyota”);

or the index can be assigned manually:

$cars[0] = “Volvo”;
$cars[1] = “BMW”;
$cars[2] = “Toyota”;

In the above example, we created an array and used cars as items.

The zero index number has value “Volvo”.

One index number has value “BMW”.

Two index number has value “Toyota”.

To loop through and print all the values of an indexed array, you could use a for loop, like this:

<?php
$cars = array(“Volvo”, “BMW”, “Toyota”);
$arrlength = count($cars);

for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
  echo “<br>”;
}
?>

PHP Associative Arrays

The Associative Array is one of the efficient arrays in PHP. The associative array is used to store in the key-value pair. In simple word, it is used to store the value using key and value pair
We define the key for the value. At the time of execution, we pass the key and It gives the related value as output .

The following syntax of the associative array will help you understand the key pair concept: key=>value .

In the above syntax, you can see that the key is making pairs with value using => sign. This shows that you have assigned the key to the value.

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”;

To loop through and print all the values of an associative array, you could use a foreach loop.

PHP Multidimensional Arrays

Previously we have described arrays that are a single list of key/value pairs.

However, sometimes you want to store values with more than one key. For this, we have multidimensional arrays.

A multidimensional array is an array containing one or more arrays.

PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage.

One of the major advantages of the multidimensional array, each element in the main array can be an array. And each element in the sub-array can be an array and also increase the same process for other elements.

We can store the data in a two-dimensional array, like this:

$cars = array (
array(“Volvo”,22,18),
array(“BMW”,15,13),
array(“Saab”,5,2),
array(“Land Rover”,17,15)
);

Now the two-dimensional $cars array contains four arrays, and it has two indices: row and column.

To get access to the elements of the $cars array we must point to the two indices (row and column):

<?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>”;
?>

We can also put a for loop inside another for loop to get the elements of the $cars array (we still have to point to the two indices):

<?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>”;
}
?>

[contact-form-7 404 "Not Found"]