PHP Array: with all type of array

PHP array is a variable which can store multiple value in a single variable. Its work with Key & Value, means each Value is associated with Unique Key or Index Number. In PHP an array() function used to create arrays variable.

Syntax of PHP Array is as follows:
$fruits = array(“Apple”, “Banana”, “Watermelon”, “Coconut”, “Pomegranate”);

There are 3 kinds of array and all each array value accessed with its Index Number.

  1. Numeric Array
  2. Associative Array
  3. Multidimensional Array

Numeric Array

An array to work with associated numeric key and can store number, string as the key value. Here key means Index Number of arrays which by default start with zero.

<?php
// Array with numeric value............
$numeric = array( 1, 2, 3, 4, 5);

foreach( $numeric as $value ) {
	echo "Value is $value </br>";
}

// Array with string value.............
$string[0] = "Apple";
$string[1] = "Banana";
$string[2] = "Watermelon";
$string[3] = "Coconut";
$string[4] = "Pomegranate";

foreach( $string as $value ) {
	echo "Value is $value </br>";
}
?>

Associative Array

Associative array working as same Numeric arrays but you can define its Index Key with string. Like in Numeric array its default key is numeric but in an associative array, each value has its defined String key.

<?php 
// Associate array first method........... 
$fruits = array("apple" => 500, "banana" => 1000, "watermelon" => 50, "coconut" => 8000, "pomegranate" => 100);

echo "Number of Apple is ". $fruits['apple'] . "</br>";
echo "Number of Banana is ".  $fruits['banana']. "</br>";
echo "Number of Watermelon is ".  $fruits['watermelon']. "</br>";
echo "Number of Coconut is ".  $fruits['coconut']. "</br>";
echo "Number of Pomegranate is ".  $fruits['pomegranate'];

// Associate array second method............
$fruits['apple'] = "Red";
$fruits['banana'] = "Yellow";
$fruits['watermelon'] = "Green";
$fruits['coconut'] = "White";
$fruits['pomegranate'] = "Red";

echo "Apple ". $fruits['apple'] . "</br>";
echo "Banana ".  $fruits['banana']. "</br>";
echo "Watermelon ".  $fruits['watermelon']. "</br>";
echo "Coconut ".  $fruits['coconut']. "</br>";
echo "Pomegranate ".  $fruits['pomegranate'];
?>

Multidimensional PHP Array

Multidimensional array work as each main array associated with sub array and a sub array associated with the next sub array. Multidimensional array work with multi indexing to show the particular value of an array. Its indexing works as Numeric array.

<?php 
$fruits = array( "apple" => array (
	   "color" => "Red",
	   "tast" => "Sweet",	
	   "quantity" => 500
	),
	
	"banana" => array (
	   "color" => "Yellow",
	   "tast" => "Sweet",
	   "quantity" => 1000
	),
	
	"watermelon" => array (
	   "color" => "Green",
	   "tast" => "Sweet",
	   "quantity" => 50
	)
);

// Accessing multi-dimensional array values........
echo "Number of Apple ". $fruits['apple']['quantity']. "</br>";
echo "Color of Banana ". $fruits['banana']['color']. "</br>";
echo "Tast of Watermelon ". $fruits['watermelon']['tast'];
?>