How to PHP Scripting- PHP Arrays

PHP Arrays are variables that stores multiple or a group of values under a single variable name.  Arrays can be used in many ways to store and organize data quickly and efficiently. It is one of the more useful data types available to any programming language. 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.

There are three different types of arrays in PHP.

1.Indexed Arrays.
2.Associative Arrays.
3. Multidimensional Arrays.


php arrays


1.Indexed PHP Arrays.

A PHP array with a numeric index is commonly called an indexed array. There are two ways to create indexed arrays. Index of elements can be assigned automatically.

PHP Array
      $seasons = array( "Autumn", "Winter", "Spring", "Summer" )

Or do it manually.

     $cars[0] = "Autumn";
     $cars[1] = "Winter";
     $cars[2] = "Spring";
     $cars[3] = "Summer";


2.Associated PHP Arrays.

An array with named positions is called an associative array. In an associative array, the keys assigned to values can be arbitrary and user defined strings.


Associated PHP array
	 $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'];     
	 echo $second_array['key3'];   
	 echo $first_array['key2'];    
	 echo $second_array['key4'];    


      the first element. 
      the first element of the second array 
      the second element
      this is the second element of the second array


3. Multidimensional PHP Arrays.

A multidimensional array is an array that contains at least one other array as the value of one of the indexes.


Multidimensional PHP array
	  $contacts = array(
			"name" => "Anwar",
			"email" => "",
			"name" => "Habeeb",
			"email" => "",
			"name" => "Muhsin",
			"email" => "",
	 echo "Anwar's Email-id is: " . $contacts[0]["email"]; //prints

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...