PHP Array


PHP Array:– In programming array is a collection of similar data having same size and types.Each value in an array is called array element.

For example you could have array of integers ,strings,characters,objects etc depending on need for programming perspective .
There are three types of php array which are discussed below one by one in detail.

  1. Indexed Array
  2. Associative Array
  3. Multidimensional Array

PHP Array | Example

An Indexed Array stores values on indexes starting fron 0 to length of array.

Example of Indexed Array

<?php
$direction_name=array("North","South","East","West");
echo "Directions are:  $direction_name[0] , $direction_name[1] , $direction_name[2] , $direction_name[3]";
?>

PHP Associative Array | Example

We can also store values in key value pair by using associative array

Example of Associative Array

<?php
$direction_name=array("National_Bird"=>"Peacock","National_Game"=>"Football","National_Currency"=>"Rupee");
echo "National Bird is ==>".$direction_name['National_Bird'];
?>

PHP Multidimensional Array | Example

Multidimentional array stores multiple arrays inside an array.

Example of Multidimensional Array

<?php
$employee=array(
array(
"Sonu"=>"Java",
"Harish"=>"Android",
),
array(
"Web Technology"=>"PHP",
"Android Technology"=>"Java",
),
);
echo "Retreiving values from nested array "  .$employee[0]["Harish"];
echo "<br>";
echo $employee[1]["Android Technology"]
?>

Advertisements

Add Comment

📖 Read More