PHP Arrays: Numeric, Associative, Multidimensional

An array is the collection of one or more values of the same type. These values are stored in a single variable. There are different types of array which we are going to discuss here.

Types of Array:-

  • Numeric Array
  • Associative Array
  • Multidimensional Array
  • In array, elements are store in key-value pairs. Each element in the array can be identified by using its key along with the name of the variable. Array (array()) function can be used to create array. Function name print_r() is used to print recursively in the array element.

Numeric Array

  • In Numeric Array, each element has a numeric key associated with it.
  • We can call this numeric key as an index. This index is represented by numbers starting from 0(zero)
Numeric Array

Syntax:-

<?php
	$arrayname = array(value1, value2,…., valueN);
?>

OR

<?php
	$arrayname[] = value1;
	$arrayname[] = value2;
	…..
	$arrayname[] = valueN;
?>

Here we understand numeric array in better way with example.

<?php
	//First Method
	$chocolates = array(“Mars”, “Dairy Milk”, “Snickers”, “Ferrero Rocher”);

	for( $i = 0; $i <= 3; $i++){
		echo $chocolates[$i] . “<br>”;
	}

	//Second method
	$chocolates[0] = “Mars”;
	$chocolates[1] = “Dairy Milk”;
	$chocolates[2] = “Snickers”;
	$chocolates[3] = “Ferrero Rocher”;

	for( $i = 0; $i <= 3; $i++){
		echo $chocolates[$i] . “<br>”;
	}
?>

Output:-
Mars
Dairy Milk
Snickers
Ferrero Rocher
Mars
Dairy Milk
Snickers
Ferrero Rocher

Associative Array

  • In Associative Array, each element having key (also termed as index) associated with it.
  • This key is represented as string, so that it can establish strong connection between key & value.
Associative Array
Associative Array

Syntax:-

<?php
	$arrayname = array(key1 => value1, key2 => value2,…., keyN => valueN);
?>

OR

<?php
	$arrayname[key1] = value1;
	$arrayname[key2] = value2;
	…..
	$arrayname[keyN] = valueN;
?>

Example:-

<?php
	//First Method
	$salary = array("Joseph" => 1500, "Volt" => 2000, "Tom" => 1000);
	
	foreach($salary as $key => $value){
		echo $key . “salary is ” . $value . “<br/>”;
	}

	//Second method
	$salary['Joseph'] = "medium";
	$salary['Volt'] = "high";
	$salary['Tom'] = "low";
         
	echo "Joseph salary is ". $salary['Joseph']. "<br />";
	echo "Volt salary is ".  $salary['Volt']. "<br />";
	echo "Tom salary is ".  $salary['Tom']. "<br />";
?>

Output:-
Joseph salary is 1500
Volt salary is 2000
Tom salary is 1000
Joseph salary is medium
Volt salary is high
Tom salary is low

Multidimensional Array

  • Nested arrays are called a multidimensional array.
  • Nested arrays mean array containing the array.
  • Using multiple indexes we can access the value in a multidimensional array.
  • Dimensions indicate a number of indices in a multidimensional array.
Multidimensional Array

Two dimensional Array:-

  • It is the collection of elements that are arranged in two-dimensional rows & columns.


Syntax:-

array ( array (elements…), array (elements…), …)

Example:-

<?php
	$numbers = array(array(10,20,30), array(40,50,60), array(70,80,90));	
	for($i = 0; $i <=2; $i++){
		for(($j = 0; $j <=2; $j++){
			echo $numbers[$i][$j] . " ";
		}
		echo "<br/>"
	}
?>

Output:-

10 20 30
40 50 60
70 80 90

Multidimensional Array:-

  • All the related data of same type are grouped together in multidimensional array.

Syntax:-

array ( array (elements…), array (elements…), …)

Example:-

<?php
	$food = array(
		"Fruits" => array("Apple", "Mango", "Banana"),
		"Vegetables" => array("Potato", "Tomato", "Onion"),
		"Pulses" => array("Dry beans", "Lentils", "Pegeon Peas")
	);
	print_r($food);
?>

Output:-

Array(
[Fruits] => Array (
[0] => Apple
[1] => Mango
[2] => Banana
)
[Vegetables] => Array (
[0] => Potato
[1] => Tomato
[2] => Onion
)
[Pulses] => Array (
[0] => Mung
[1] => Chana
[2] => Tuver
)
)