Arrays In PHP
Introduction to Arrays : Array is a collection of key/value pairs. A key/index can be integer or string. Keys can be non-sequential. Values can be of dissimilar data type. Arrays are prefixed by '$'. No limit on number of elements.
#Example $a[0]=100; $a[10]="abc"; $a["xyz"]=200;
Array Initialization
#Valid example of Array Initialization $myarray=array(10,20,30); $myarray[0]=10; $myarray["abc"]=20; $myarray[2]=10; $myarray[0]=10; $myarray[10]=20; $myarray[]=30; #index/key will be 11 #Initialization – Explicit keys $myarray=array("class1"=>10, "class2"=> 20, "class3"=> 30); $myarray=array(10=>100, "class2"=> 200, 300); # index for 300 is 11 , highest numerical index + 1
Array of Arrays
#Example $Team=array( "India" => array("Sachin", "Dravid", "Ganguly", "Shewag"), "Australia"=>array("Hayden", "Ponting", "Warne") ); echo $Team["India"][0]; #Sachin echo $Team["India"][2]; #Ganguly echo $Team["Australia"][0]; #Hayden echo $Team["India"]; #Array
Array Function
#Syntax Array( [key1]=>value1 [key2]=>value2…..) #Example $arrEg=array("India"=>"New Delhi", "Sri Lanka"=>"Colombo", "Bangladesh"=>"Dhaka"); print_r($arrEg);
#Example $arrEg=array("India"=>"New Delhi", "Sri Lanka"=>"Colombo", "Bangladesh"=>"Dhaka"); if( is_array($arrEg)) echo "Is array";
#Example $states=array("Delhi","Karnataka"); echo array_unshift($states,"Maharashtra"); print_r($states); # Output: Maharashtra, Delhi, Karnataka
#Example $states=array("Delhi","Karnataka"); echo array_push($states,"Maharashtra"); print_r($states); # Output: Delhi, Karnataka, Maharashtra
#Example $states=array("Delhi","Karnataka"); echo array_shift($states,); print_r($states); #Output Delhi Karnataka
#Example $states=array("Delhi","Karnataka"); echo array_pop($states,); print_r($states); #Output Karnataka Delhi
#Example $find_element="Delhi"; $states=array("Delhi","Karnataka"); if(in_array($find_element,$states)) echo "Yes, it is present in array"; #Output Yes, it is present in array
#Example $states=array("Delhi","Karnataka"); if(array_key_exists("Delhi",$states)) echo "Yes, Delhi is a key in states array"; else echo "Not present"; #Output Not present
Other Functions
Function | Use |
---|---|
count() | Returns number of elements in the array |
array_keys() | Can be used to fetch the keys present in the array |
array_values() | Returns all the values of the respective keys present in the passed array |
array_count_values() | Returns the values and the frequency of each value in form of an array |
array_reverse() | Rearranges the array elements in the reverse order |
array_flip() | Is used to convert the keys to values and values to keys |
sort() | Is used to sort the array |
Array Traversal Functions
Function | Use |
---|---|
key() | Used to fetch the key in the present position of the array |
current() | Returns the value of current key in the array |
next() | Returns the value of the key immediately after the current key (pointer location will also be moved) |
prev() | Returns the value of the preceding key |
reset() | Used to set the array pointer to the value of starting key |
end() | Used to set the array pointer to the value of last key |
#Example $arr=array('One'=>"Karnataka","Two"=>"Maharashtra","Three"=>"AP"); $nextVal=next($arr); echo $nextval; #Maharashtra $prevVal=prev($arr); echo $prevval; #Karnataka $endVal=end($arr); echo $endval; #AP $firstVal=reset($arr); echo $firstval; #Karanataka