Monday, August 20, 2012

PHP Array

These are a series of like values that are assigned to an individual variable with each individual value in the array being referenced with a unique identifier. Once these values are stored into an array, it is possible to retrieve individual values or to perform multiple actions against the stored values. The following demonstrates the storing and retrieval of an array set:

An array can be used when we want a single variable to hold multiple values. Values in arrays can be accessed by using the index associated to it. Each element in the array has its own index. Default start of index starts from 0. Arrays are useful when you want to store a group of data.

Array Example 1
 <?php  
 $Country[0] = "Australia"; // Australia is the first(0) value  
 $Country[1] = "Russia"; // Russia is the second(1) value  
 $Country[2] = "Germany"; // Germany is the third(2) value  
 $Country[3] = "France"; // France is the fourth(3) value  
 // The following will display: Let's go to Germany!  
 print "Let's go to $Country[2]!";  
 ?>  
Demo

The above example could be written less explicitly:

 <?php  
 $Country = array("Australia", "Russia", "Germany", "France");  
 print "Let's go to $Country[2]!";  
 ?>  
Demo

Array Example 2

Rather than using numeric identifiers, array identifiers can be more meaningful when created as in
the following examples:
 <?php  
 $Capital["Australia"] = "Canberra";  
 $Capital["Russia"] = "Moscow";  
 $Capital["Germany"] = "Berlin";  
 $Capital["France"] = "Paris";  
 print "The capital of Russia is ".$Capital["Russia"]."!";  
 ?>  
 <?php  
 $Capital = array(  
 "Australia" => "Canberra",  
 "Russia" => "Moscow",  
 "Germany" => "Berlin",  
 "France" => "Paris");  
 print "The capital of Russia is ".$Capital["Russia"]."!";  
 ?>  
Demo

Array Example 3

 <?php   
                // defining a simple array  
                $array1 = array(4,8,15,16,23,42);  
                // referencing an array value by its index  
                echo $array1[0];  
                // arrays can contain a mix of strings, numbers, even other arrays  
                $array2 = array(6,"fox", "dog", array("x", "y", "z"));  
                // referencing an array value that is inside another array  
                echo $array2[3][1];  
           ?>  
           <br />  
           <?php  


Demo

Array Example 4

 <?php   
                // defining a simple array  
                $array1 = array(4,8,15,16,23,42);  
                // referencing an array value by its index  
                echo $array1[0];  
                // arrays can contain a mix of strings, numbers, even other arrays  
                $array2 = array(6,"fox", "dog", array("x", "y", "z"));  
                // referencing an array value that is inside another array  
                echo $array2[3][1];  
           ?>  
           <br />  
           <?php  

Demo

Array Example5

 <?php  
                // You can also assign labels to each pocket (called "keys"),  
                $array3 = array("first_name" => "Kevin", "last_name" => "Skoglund");  
                // which will allow you to use the key to reference the value in that array position.  
                echo $array3["first_name"] . " " . $array3["last_name"] . "<br />";  
                $array3["first_name"] = "Larry";  
                echo $array3["first_name"] . " " . $array3["last_name"] . "<br />";  
           ?>  

Demo

Array Example 6


        <?php  
                // Changing values in an array that has already been defined  
                // It's just like variables but you use the index to reference the array position  
                $array2[3] = "cat";  
                echo $array2[3];  
           ?>  

Demo


 

No comments:

Post a Comment