Introduction of PHP Array



An array is a kind of special variable / data structure, which can hold one or more similar type of values in a single value at a time. Suppose you have 200 numbers of items and you want to store them, so by defining a single array of 200 length is easier than defining 200 individual variables.

Note : Short array syntax replaces array() with [].

<?php
$ewa_array = array(
	"ewa" => "blog",
	"blog" => "ewa",
);

// Using the short array syntax
$ewa_array = [
	"ewa" => "blog",
	"blog" => "ewa",
];
?>

There are mainly 3 types of Array in PHP:

– Numeric array / Indexed array
– Associative array
– Multidimensional array

See the detailed about these types of array below:

Numeric array / Indexed array − In Numeric array it is also known as indexed array as well, index will be represented by numbers and by default array index starts from zero.

<?php

/* First method to create numeric array. */
$numbers_array = array( 7, 8, 9, 10, 12);

foreach( $numbers_array as $number ) {
	echo "Array Value is : $number <br />";
}

/* Second method to create numeric array. */
	$numbers_array[0] = "seven";
	$numbers_array[1] = "eight";
	$numbers_array[2] = "nine";
	$numbers_array[3] = "ten";
	$numbers_array[4] = "twelve";

foreach( $numbers_array as $number ) {
	echo "Array Value is : $number <br />";
}
?>

Output will be :

Array Value is : 7
Array Value is : 8
Array Value is : 9
Array Value is : 10
Array Value is : 12
Array Value is : seven
Array Value is : eight
Array Value is : nine
Array Value is : ten
Array Value is : twelve

– Associative array − In Associative array their index as string like key and values. It is similar to the Numeric array only difference in terms of index. This array is generally used to store the relational data like students with their respective class then we can have student names as keys and class would be the value.

<?php
 /* associative array example 1*/
 $age = array("anil" => 28, "suraj" => 22, "kanav" => 5);
 
 echo "Age of Anil is : ". $age['anil'] . " Years"."<br />";
 echo "Age of Suraj is : ".  $age['suraj']. " Years". "<br />";
 echo "Age of Kanav is : ".  $age['kanav']. " Years". "<br />";
 
 /* associative array example 2 */
 $age['anil'] = "high";
 $age['suraj'] = "medium";
 $age['kanav'] = "low";
 
 echo "Age of Anil is : ". $age['anil'] . "<br />";
 echo "Age of Suraj is : ".  $age['suraj']. "<br />";
 echo "Age of Kanav is : ".  $age['kanav']. "<br />";
?>

Output will be :

Age of Anil is : 28 Years
Age of Suraj is : 22 Years
Age of Kanav is : 5 Years
Age of Anil is : high
Age of Suraj is : medium
Age of Kanav is : low

– Multidimensional array − Array in array means, array containing one or more arrays, In this kind of array values are accessed using multiple indices.

In this example I am creating a 2 dimensional array to store employee details of 3 employees :

<?php
	 $employee_details = array( 
		"suraj" => array (
		   "age" => '25',
		   "contactno" => '88767546',	
		   "salary" => '35000'
		),
		
		"sonu" => array (
		   "age" => '28',
		   "contactno" => '1234578658',
		   "salary" => '28000'
		),
		
		"manoj" => array (
		   "age" => '31',
		   "contactno" => '0987656789',
		   "salary" => '55000'
		)
	 );
	 
	 /* Accessing multi-dimensional array values */
	 echo "Salary of Suraj :- " ;
	 echo $employee_details['suraj']['salary'] . "<br />"; 
	 
	 echo "Age of Sonu :- ";
	 echo $employee_details['sonu']['age'] . "<br />"; 
	 
	 echo "Contact Number of Manoj :- " ;
	 echo $employee_details['manoj']['contactno'] . "<br />"; 
?>

The Output will be given below:

Salary of Suraj :- 35000
Age of Sonu :- 28
Contact Number of Manoj :- 0987656789

Hope this article helps you in PHP development and understandings of Array. Stay tuned for more PHP Tutorials, Hope you enjoyed reading, if you need the professional PHP, PHP Framework and Magento 2 Development we can help you, just Click on the Link and send me your requirements.