Tuesday 4 February 2014

Top Five Most Used PHP Array Functions for Beginners


Array Functions in PHP There is more than fifty array functions available in PHP.
In this Article Iam explain 5 popular array functions




1). is_array() function
syntax: is_array($argument)
It returns true if the $argument is an array otherwise returns false.
$argument = array('value1','value2');
 if(is_array($argument)){

 echo 'array';

 } else {

 echo 'Not an array';

 }

 // Output:- array
2). in_array() function
syntax: in_array($search_value,$array,type);
in_array() function searches an array for a specific value,Returns true if it present otherwise false.
type is optional part in in_array function,If the search parameter is a string and the "type" parameter is set to TRUE, the search is case-sensitive.

$friends = array('srinu','karthik','suneel','Bhaskar'); 

 if(in_array("Bhaskar",$friends)){

 echo 'value found in an array';

 }else{
         echo 'value not found';
              }

        //Output:- value found in an array
3). array_merge() function
syntax:array_merge($array1,$array2);
This function merges one or more arrays into one array.
$first_array = array('car','bus');

 $second_array = array('train','flight');

 $array_merge_result = array_merge($first_array,$second_array);

 print_r($array_merge_result);

 Output:-
 Array
 (
     [0] => car
     [1] => bus
     [2] => train
     [3] => flight
 )
4).list() function
This function is used to assign values to a list of variables in one operation.
$testarray = array("Dog","Cat","Horse");

   list($a, $b, $c) = $testarray;

   echo $a;   //Dog
   echo $b;   //Cat
   echo $c;   //Horse
5).count() function
It returns the number of elements in an array.
$testarray = array('one','two','three');

 echo count($testarray);

 //output:- 3

No comments:

Post a Comment