Array Basics - PHP Example

Array is a collection of similar types of data types.
Array is used to stores multiple values in one place.
Array contains key => value pair, each value in array assigned on a key (index).
We can fetch stored value with the use of key (index).

Create an Array:

Php array() function is used to create a Array.
We can store any number of datatype (string or integer or decimals) values in array.
By default array will store values on index start with value 0 then 1 and so on.

<?php
 
   // Intialize  array
   $Array_Create = array();
  
   // Store string values in array
   $Array_Create[0] = "php";
   $Array_Create[1] = "example";
   $Array_Create[2] = "string";
   
   // Intialize and store value in array
   $Array_Create = array("learning","development","tutorials");
   
 ?>

Print php array on screen:

 <?php
 
    // Intialize and store value in array
    $Print_Array = array("php","example","source code");
  
    // Mainly use these two php functions to debug array when array contain large data 
	
    // print_r() will show array values with structure
     print "<pre>";
	 print "print_r() function output: ";
     print_r($Print_Array);
     print "</pre>";
	 
	  // var_dump() will show array values with structure and datatypes 
	 print "<pre>";
	 print "var_dump() function output: ";
	 var_dump($Print_Array);
	 print "</pre>";
   
 ?>
 
------------ ****** ------------
OUTPUT:

        print_r() function output:
		Array
		(
			[0] => php
			[1] => example
			[2] => source code
		)
		var_dump() function output:
		
		array (size=3)
		0 => string 'php' (length=3)
		1 => string 'example' (length=7)
		2 => string 'source code' (length=11)
		
		
 

We can define three types of Arrays in Php :

1. Indexed Array ==> Arrays with a numneric values
2. Associative Array ==> Arrays with key and its value form
3. Multidimensional Array ==>Array containing one or more than one Array

1. Indexed Array

Indexed array values will contain numeric key (index).
If we had not given array index value then index (key) will always starts at 0.

Example:

<?php
	 
		// The index can be assigned automatically//
		$indexed_array = array("Indexed","array","example");
		
		echo $indexed_array[0];
		echo "<br>";
		
		echo $indexed_array[1];
		echo "<br>";
		
		echo $indexed_array[2];
		echo "<br>";
		echo "<br><br>";
		
		
?>

------------ ****** ------------
OUTPUT:
         Indexed
         array
		 example
		 
		 

Example:

<?php
	
	   // We can also give own index
		$user_array = array();
		
		$user_array[10] = 12;
		$user_array[20] = 13;
		$user_array[40] = 15;
		
		print "<pre>";
		print_r($user_array);
        print "</pre>";
		
	
?>


------------ ****** ------------
OUTPUT:

        Array
		(
			[10] => 12
			[20] => 13
			[40] => 15
		)
		
		

Example: Fetch Array values using loop

<?php
	 
		//  Array index will assign automatically
		$indexed_array = array("array","fetch","data","loop");
		
		// Get array length by php count() function
		$size  = count($indexed_array);
		
		for($i = 0; $i < $size ; $i++)
		{
		   print "<br>";   // each output show in new line
		   print "Array values at index $i = ".$indexed_array[ $i ];
		}
		
?>


------------ ****** ------------
OUTPUT:

		Array values at index 0 = array
		Array values at index 1 = fetch
		Array values at index 2 = data
		Array values at index 3 = loop
		
		

Example: Another way to Fetch Array values using loop (By foreach() php function)

 <?php
	 
		// Array index assigned automatically
		$loop_array = array("array","for","each","loop");
		
		foreach($loop_array as $key => $value)
		{
		   print "<br>";   // each output show in new line
		   print "Array values at index $key = ".$value;
		}
		
?>

------------ ****** ------------
OUTPUT:

       Array values at index 0 = array
	   Array values at index 1 = for
	   Array values at index 2 = each
	   Array values at index 3 = loop

2. Associative Array :

As we know , in indexed array we have stored values on integer value as the key.

In Associative Array, we can store value on a user defined datatype (integer,string etc..) key.

SYNTAX:

	     array( 
		      “Key1” => “Value1” ,
			  “Key2” => “Value2” 
			  , , , , , , 
			 );
   

Example:

<?php
 
	   // Intialize  array
	   $Array_Create = array();
	  
	   // Store string values in array
	   $Array_Create['language'] = "php";
	   $Array_Create['example']  = "associative";
	   $Array_Create['demo']     = "array";
	   
	     print "<pre>";
		 print "print_r() function output: ";
		 print_r($Array_Create);
		 print "</pre>";
	   
	   // Another Way to intialize and fill values to associative array 
	   // Intialize and store value in array
	   $Array_Create = array("lession" => "array", "website" => "TemplateOnWeb", "Year" => 2016);
	   
	     print "<br><pre>";
		 print "print_r() function output: ";
		 print_r($Array_Create);
		 print "</pre>";
	   
?>

------------ ****** ------------
OUTPUT:
         print_r() function output:
			Array
			(
				[language] => php
				[example] => associative
				[demo] => array
			)
			
		print_r() function output:
			Array
			(
				[lession] => array
				[website] => TemplateOnWeb
				[Year] => 2016
			)
			

Example:Fetch Associative Array values Using loop ( By foreach() php function )

<?php
	 
		// Intialize and store value in array
	   $Array_Create = array("name" => "PhpOnWeb", "work" => "Web Development", "Team" => 15);
		
		foreach($Array_Create as $key => $value)
		{
		   print "<br>";   // each output show in new line
		   print $key." => ".$value;
		}
		
?>

------------ ****** ------------
OUTPUT:
         name => PhpOnWeb
		 work => Web Development
	     Team => 15

3. Multidimensional Array

We can store one or more new array at any array key.

Example  :

 <?php
	 
		// Intialize array
	    $MultiDimensional_Array = array();
		
		$MultiDimensional_Array[0] = array('php','multidimensional','array','example');
		$MultiDimensional_Array[1] = array(11,12,13);
		$MultiDimensional_Array[2] = array(21,22,23);
		
		 print "<br>";  // New Line
		  
		  // Print Multidimensional array structure
		 print "<pre>";
		 print_r($MultiDimensional_Array);
		 print "</pre>";
		 
		 print "<br>";  // New Line
		 
		 print "Fetch value from array : ";
		 
		 print "<br>"; // New Line
		 
		 print "Name1 : ". $MultiDimensional_Array[0][0];
		 
		 print "<br>"; // New Line
		 
		 print "Name2 : ". $MultiDimensional_Array[0][1];
		 
		 print "<br>"; // New Line
		 
		 print "Name3 : ". $MultiDimensional_Array[0][2];
		 
		 print "<br>"; // New Line
		 
		 print "Name4 : ". $MultiDimensional_Array[0][3];
		 
?>

------------ ****** ------------
OUTPUT:
     	Array
		(
			[0] => Array
						(
							[0] => php
							[1] => multidimensional
							[2] => array
							[3] => example
						)
			[1] => Array
						(
							[0] => 11
							[1] => 12
							[2] => 13
						)
			[2] => Array
						(
							[0] => 21
							[1] => 22
							[2] => 23
						)
		 )    

Example:

Now we are going to store below table in Multidimensional array.

Name Dept Age
John Website 45
Sheebu Developement 32
Mack Internet 56

Example  :

<?php
	 
		// Intialize and store value in array
	    $MultiDimensional_Array = array(
		                                 'Name' => array('John','Sheebu','Mack'),
										 'Dept' => array('Website','Developement','Internet'),
										 'Age' => array(45, 32, 56),
		                               );
		
		 print "<br><pre>";
		 print_r($MultiDimensional_Array);
		 print "</pre>";
		
?>

------------ ****** ------------
OUTPUT:
		Array
		(
			[Name] => Array
			(
				[0] => John
				[1] => Sheebu
				[2] => Mack
			)
			[Dept] => Array
			(
				[0] => Website
				[1] => Developement
				[2] => Internet
			)
			[Age] => Array
			(
				[0] => 45
				[1] => 32
				[2] => 56
			)
		)        

Sorting Arrays  :

Array elements can be sorted in ascending or descending order.

In php, some functions are metioned for array shorting.

These functions are :

1. sort() – sort arrays elements in ascending order

2. rsort() – sort arrays elements in descending order

3. asort() – sort associative arrays values in ascending order

4. ksort() – sort associative arrays keys (index) in ascending order

5. arsort() – sort associative arrays values in descending order

6. krsort() – sort associative arrays keys in descending order

1. sort array by sort() php function :

Example:

 <?php
 
       $key = array("short","array","basics");
	   
	  // php function for array
	   sort($key) ;
	   
	     print "<br><pre>"
		 print_r($key);
		 print "</pre>"
		 
 ?>

------------ ****** ------------
OUTPUT: 
		 Array
			(
				[0] => array
				[1] => basics
				[2] => short
			)
			

2. sort array by rsort() php function :

Example:

<?php

       $key = array("rshort","array","function");
	   
	  // php function for array
	   rsort($key) ;
	   
	     print "<br><pre>"
		 print_r($key);
		 print "</pre>"
		 
 ?>
 
------------ ****** ------------
OUTPUT: 
        Array
		(
			[0] => rshort
			[1] => function
			[2] => array
		)

3. sorting associative array by asort() php function :

Used to short associative array values in ascending order.

Example :

<?php

       $key = array(
	                 "1st" => "ashort",
					 "2nd" => "array",
					 "3rd" => "shorting");
	   
	  // php function for array
	   asort($key) ;
	   
	     print "<br><pre>";
		 print_r($key);
		 print "</pre>";
?>
 
------------ ****** ------------
OUTPUT: 
        Array
		(
			[2nd] => array
			[1st] => ashort
			[3rd] => shorting
		)
		

4. sorting associative array keys by ksort() php function :

Used to short associative array keys (index) in ascending order.

Example :

<?php

       $key = array(
	                 "lion" => "zebra",
					 "bird" => "insect",
					 "monkey" => "banana"
					);
	   
	  // php function for array
	   ksort($key) ;
	   
	     print "<br><pre>"
		 print_r($key);
		 print "</pre>"
		 
 ?>
  
------------ ****** ------------
OUTPUT:
		Array
		(
			[bird] => insect
			[lion] => zebra
			[monkey] => banana
		)

 

5. sorting associative array values by arsort() php function :

Used to short associative array values in descending order.

Example:

<?php

       $key = array(
	                 "lion" => "zebra",
					 "bird" => "insect",
					 "monkey" => "banana",
				 );
	   
	  // php function for array
	   arsort($key) ;
	   
	     print "<br><pre>"
		 print_r($key);
		 print "</pre>"
		 
 ?>
   
------------ ****** ------------
OUTPUT:
        Array
		(
			[lion] => zebra
			[bird] => insect
			[monkey] => banana
		)
 
 

6. sorting associative array values by krsort() php function :

Used to short associative array keys (index) in descending order.

Example:

<?php

       $key = array(
	                 "lion" => "zebra",
					 "bird" => "insect",
					 "monkey" => "banana",
					 );
	   
	  // php function for array
	   krsort($key) ;
	   
	     print "<br><pre>"
		 print_r($key);
		 print "</pre>"
		 
 ?>
    
------------ ****** ------------
OUTPUT:
        Array
		(
			[monkey] => banana
			[lion] => zebra
			[bird] => insect
		)