Arrays in PHP


An ordered map in PHP is called an array (contains value based on the key). It's a variable that may hold several values of the same type.

PHP Array Less Code Benefits: Multiple variables do not need to be defined.Simple to traverse: We can traverse all the elements of an array with a single loop.Sorting: The items of an array can be sorted.

Array Types in PHP.In PHP, there are three types of arrays.

  • Array with indexes
  • Array of Associative
  • Array with many dimensions

PHP Indexed Array

A PHP index is a number that starts at 0 and goes up from there. We may store numbers, strings, and objects in the PHP array, and by default, all PHP array items are given an index number.

The term "indexed array" can be defined in two ways:

1st way:

  • $season=array("summer","winter","spring","autumn");

2nd way:

  • $season[0]="summer";
  • $season[1]="winter";
  • $season[2]="spring";
  • $season[3]="autumn";

Example

<?php $season=array("summer","winter","spring","autumn"); echo "Season are: $season[0], $season[1], $season[2] and $season[3]"; ?>

Output:

Season are: summer, winter, spring and autumn

File: array2.php

<?php $season[0]="summer"; $season[1]="winter"; $season[2]="spring"; $season[3]="autumn"; echo "Season are: $season[0], $season[1], $season[2] and $season[3]"; ?>

A data structure that holds one or more similar values in a single value is known as an array. If you wish to save 100 numbers, for example, instead of setting up 100 variables, you may announce a list of 100 lengths.

There are three types of arrays, and each array value is accessed by an ID c, also known as the array index.

An array having a number index is known as a numeric array. Linearly, values are stored and retrieved.

An array using strings as the index is called an associative array. Instead of storing element values in a strict linear index sequence, this saves them in conjunction with key values.

An array comprising one or more arrays and values is referred to as a multidimensional array.

Numeric Array

Numbers, strings, and any other object can be stored in these arrays, but numbers represent their indexes. The array index starts at zero by default.

Example

The example below shows how to construct and access numeric arrays. To construct an array, we utilized the array() method. The function reference explains how to use it.

To construct an array, we utilized the array() method. The function reference explains how to use it.

<html> <body> <?php /* First method to create array. */ $numbers = array( 1, 2, 3, 4, 5); foreach( $numbers as $value ) { echo "Value is $value <br />"; } /* Second method to create array. */ $numbers[0] = "one"; $numbers[1] = "two"; $numbers[2] = "three"; $numbers[3] = "four"; $numbers[4] = "five"; foreach( $numbers as $value ) { echo "Value is $value <br />"; } ?> </body> </html>

This will produce the following result −

Value is 1 Value is 2 Value is 3 Value is 4 Value is 5 Value is one Value is two Value is three Value is four Value is five

Associative Arrays

In terms of functionality, associative arrays are pretty similar to numeric arrays. However, they differ in terms of the index. The associative array index will be a unit of characters, allowing you to create a solid link between keys and value.

A numerically indexed array would not be ideal for storing employee wages in an array. Instead, we might use the workers' names as keys in an associative array, with their salaries as the value.

NOTE:When printing, do not put the associative array within a double quotation; otherwise, it will not return any value.

Example

<html> <body> <?php /* First method to associate create array. */ $salaries = array("Riya" => 1000, "Amit" => 500, "zara" => 500); echo "Salary of Riya is ". $salaries['Riya'] . "<br />"; echo "Salary of Amit is ". $salaries['Amit']. "<br />"; echo "Salary of zara is ". $salaries['zara']. "<br />"; /* Second method to create an array. */ $salaries['Riya'] = "high"; $salaries['Amit'] = "medium"; $salaries['zara'] = "low"; echo "Salary of Riya is ". $salaries['Riya'] . "<br />"; echo "Salary of Amit is ". $salaries['Amit']. "<br />"; echo "Salary of zara is ". $salaries['zara']. "<br />"; ?> </body> </html>

This will produce the following result −

Salary of Riya is 1000 Salary of Amit is 500 Salary of zara is 500 Salary of Riya is high Salary of Amit is medium Salary of zara is low

Multidimensional Arrays

Each element in the primary array can also be an array in a multidimensional array. And each sub-array element can be an array, and so on. Multiple indexes are used to access values in the multidimensional array.

Example

We construct a two-dimensional array in this example to record the grades of three students in three topics. Although this is an associative array, you can create a numeric array in the same way.

<html> <body> <?php $marks = array( "mohammad" => array ( "physics" => 35, "maths" => 30, "chemistry" => 39 ), "qadir" => array ( "physics" => 30, "maths" => 32, "chemistry" => 29 ), "zara" => array ( "physics" => 31, "maths" => 22, "chemistry" => 39 ) ); /* Accessing multi-dimensional array values */ echo "Marks for mohammad in physics : " ; echo $marks['mohammad']['physics'] . "<br />"; echo "Marks for qadir in maths : "; echo $marks['qadir']['maths'] . "<br />"; echo "Marks for zara in chemistry : " ; echo $marks['zara']['chemistry'] . "<br />"; ?> </body> </html>

output:

Marks for Riya in physics : 30 Marks for Amit in maths : 33 Marks for zara in chemistry : 39