Friday, March 13, 2015

PHP Array , print array php

Array in PHP is an ordered map, an ordered map that associates values to keys. It can hold ore then one value at a time. It can hold many values under a single name and also you can access these values by referring to an index number.

Syntax:
 $variable_name = array(value1,value2,value3); 

Array are categorised bades in dimension and keys



Array based on Dimensions:

Single Dimension:
Single dimension array have single index value. Singe dimension array are used to represent and store data in linear form called as single or one dimensional array.


Example:

PHP Script (singledimen.php)
 <?php  
 $website = array("blog", "www.nirajanghimireyblogspot.com","PHP");  
 echo "I like ".$website[0]." site having URL ".$website[1]." for ".$website[2]." course.";  
 ?>



Multi Dimension:
In multi dimension array each element in the main array can also be an array, hence it’s like an array consisting another array. Values in multi dimension array are accessed using multiple index.

Example: 

PHP Script (multidimensional.php)
 <?php  
 $website = array("blog", "www.nirajanghimireyblogspot.com",array("PHP","ASP","Other"));  
 echo "I like ".$website[0]." site having URL ".$website[1]." for ".$website[2][0]." course.";  
 ?> 

Array based on Keys :


Numeric array:

This  arrays can store numbers,  strings and any object but index are represented by numbers. In this array the default number is started from zero.

Example: 

PHP Script (numeric.php)
 <?php  
 $age = array('Joe' =>'23' ,'Andy'=>'27','Lakhan'=>'25' );  
 echo "Age of Joeis ".$age['Joe']." years old.";  
 ?>  


Associative array:

Numeric array and are very similar to numeric arrays, but they have their index as a string and association between  key and values are strong.

Example :


PHP Script (associative.php)
 <?php  
 $stdent=array(  
  "info"=>array("name"=>"Joe","dob"=>"2049"),  
  "marks"=>array("english"=>array("20","98"),"math"=>"50","science"=>"90"),"division"=>array("distic"=>"80","first"=>"60","2nd"=>"45"));  
 echo $stdent['division']['distic'];  
 ?> 

No comments:

Post a Comment