as

Write a PHP script to demonstrate array.

Write a PHP script to demonstrate array. .  

ARRAY

An array can be defined as a variable that can store many values, each value within the array is index by a number.  Array basically a compound data type. An array is collection of data values.
We have used array() function to create array.
There are three types of Array in PHP .
1. Indexed Array
2. Associative Array
3. Multidimensional (2d) Array



1. Indexed Array :-
  Indexed array is an array with is an numeric index. It is also called as numeric array. In index array key is an a  integer and begin at zero.
           These array can store number, string and any object but there index will be represented by number.

Code :-
<?php
echo "<h3>Indexed Array Demo </h3>";
$data=array(10,3.5,true,"Hello");
echo "<h4>Indexed Array is using print_r( ) </h4>";
print_r($data);
echo "<h4>Indexed Array print using for loop </h4>";
foreach( $data as $value ) 
echo "Value is $value <br>"; 
}
?>
Output :-

Indexed Array Demo

Indexed Array is using print_r( )

Array ( [0] => 10 [1] => 3.5 [2] => 1 [3] => Hello )

Indexed Array print using for loop

Value is 10
Value is 3.5
Value is 1
Value is Hello

2. Associative Array :- 
These type of is similar to numeric array in terms of functionality, but they are different in terms of their index.   Associative array will have their index as string so that you can establish a strong association between key and value.

Code :-
<?php
  echo "<h3>Associate array demo</h3>";
  $cities = array("MH" => "Jalgaon" ,"GJ" => "Gujrat", "RJ" => "Rajastan");
echo "<h4>Associate Array is using print_r( ) </h4>";
print_r($cities);
echo "<h4>Associate Array is using Key </h4>";
echo $cities['MH']."<br>";
echo $cities['GJ']."<br>";
echo $cities['RJ']."<br>";
?>
Output :-

Associate array demo

Associate Array is using print_r( )

Array ( [MH] => Jalgaon [GJ] => Gujrat [RJ] => Rajastan )

Associate Array is using Key

Jalgaon
Gujrat
Rajastan

3. Multidimensional Array :- 
In multidimensional array each element in main array can also be an array and each element in the sub-array can be an array and so on. Value in the multidimensional array is accessed using multiple indexes.

Code :-

<?php
echo "<h3>Multidimensional array demo</h3>";
$marks = array("ram" => array("phy" => 50, "math" => 38, "chem" => 67),
      "sham" => array("phy" => 85, "math" => 45, "chem" => 75),
      "raj" => array("phy" => 70, "math" => 55, "chem" => 65));
echo "Marks for Ram  : <br>" ;
echo $marks['ram']['phy']."<br>";
echo $marks['ram']['math']."<br>";
echo $marks['ram']['chem']."<br>";
echo "Marks for sham : <br>";
echo $marks['sham']['phy']."<br>";
echo $marks['sham']['math']."<br>";
echo $marks['sham']['chem']."<br>";
echo "Marks for raj : <br>" ;
echo $marks['raj']['phy']."<br>";
echo $marks['raj']['math']."<br>";
echo $marks['raj']['chem']."<br>";
?>
Output :-

Multidimensional array demo

Marks for Ram :
50
38
67
Marks for sham :
85
45
75
Marks for raj :
70
55
65