PHP: Arrays

Arrays can most easily be described as an ordered list of elements. You can access the individual elements by referring to their index position within the array. The position is either specified numerically or by name. An array with a numeric index is commonly called an indexed array while one that has named positions is called an associative array. In PHP, all arrays are associative, but you can still use a numeric index to access them.

Numeric Array

These arrays can store numbers, strings and any object but their index will be prepresented by numbers. By default array index starts from zero.


An Example of an indexed Array:

$seven = 7; 
$arrayname = array( "this is an element", 5, $seven ); 

echo $arrayname[0];   //prints: this is an element 
echo $arrayname[1];   //prints: 5 
echo $arrayname[2];   //prints: 7

#print all element of  array using loop
foreach($arrayname  as  $a)
    echo "<br/>".$a;

Associative Arrays

The associative arrays are very similar to numeric arrays in term 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 values.

To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.

NOTE: Don't keep associative array inside double quote while printing otheriwse it would not return any value.


$first_array = array("key1" => "the first element", "key2" => "the second element"); 

$second_array = array( 
    "key3" => "this is the first element of the second array", 
    "key4" => "this is the second element of the second array", 
echo $first_array['key1'];   //prints "the first element." 
echo $second_array['key3'];  //prints "the first element of the second array" 
echo $first_array['key2'];   //prints "the second element" 
echo $second_array['key4'];  //prints "this is the second element of the second array" 

#print all element
foreach($first_array as $key=>$value)
echo "<br/>".$key."=".$value;    

Two Dimensional Arrays

<table class="table table-hover table-bordered" style="width:300px; margin:auto">
array("TFT- LG","100",4500), 
array("TFT Sony","8",4200), 
array ("LCD - XEROX","5","3950"),
array("LED- Samsung","18",5100),
array("CRT - Intex","90",1200),

		 for( $r=0; $r<6;$r++)
			  echo "<tr>";	
			  for(  $c=0; $c<3;$c++ )
				<td style='text-align:right'> <?php  echo $mobile[$r][$c] ?> </td> 

			  echo "</tr>";
	     <td style='text-align:right'>  Total</td>
    	 <td style='text-align:right'> <?php echo $qty ?> </td>
         <td style='text-align:right'> <?php echo $price ?> </td>


Multidimensional Arrays

A multidimensional array is an array that contains at least one other array as the value of one of the indexes. Example below shows how to use multidimensional array:


//Initialize the array using the array() function.
$flower_shop = array( 
"rose" => array( "5.00", "7 items", "red" ), 
"daisy" => array( "4.00", "3 items", "blue" ), 
"orchid" => array( "2.00", "1 item", "white" ), 

//print "rose costs 5.00, and you get 7 items." 
echo "rose costs ".$flower_shop['rose'][0].", and you get
//print "daisy costs 4.00, and you get 3 items." 
echo "daisy costs ".$flower_shop['daisy'][0].", and you get
//print "orchild costs 2.00, and you get 1 item. 
echo "orchid costs ".$flower_shop['orchid'][0].", and you get