Array functions in PHP

There are many array functions in PHP which can be used to manipulate the array elements. Some of them can be used directly on the array elements such that you don't have to write code for it. Though there are a lot of array functions I will write some of the important ones.

1) in_array() function: in_array function is used to check whether the element is present in an array or not if it is present then it will return true otherwise false.

example :

<?php
$starwars=array("princess"=>"leia","teacher"=>"Yoda","bad guy"=>"Darth");
echo in-array("Darth",$starwars);       // returns true
echo in_array("obi-san",$starwars);    //returns false
?>

2) array_search() function : array_search function is used to search an array for given value and returns its corresponding key.

example:

<?php
$sort=array("Bubble"=>"sort1","Selection"=>"sort2","shell"=>"sort3","Radix"=>"sort4");
echo array_search("sort4",$sort);        // returns "Radix"
?>

3) array_push() function : array_push function is used to insert element in an array.The element which is inserted will be the last element of the array.

example :

<?php

$superheroes=array("Batman","Captain America","ironman","Superman","Spiderman");
array_push($superheroes,"Hulk");
echo $superheroes;                          // "Hulk" will be the last element in an array $superheroes

Similarly, we have array_pop() function which will pop the last element from an array.

4) array_shift() function : array_shift function is used to shift the elements of array.The shifting takes place towards the leftmost element is the first element and it is removed from the array.

example : 

<?php
$superheroes=array("Batman","Captain America","ironman","Superman");
echo array_shift($superheroes);
// output will be ("Captain America","ironman","Superman")

5) array_slice() function : array_slice function is used to extract the portion of array.It returns the element of the array specified by offset and length parameters.

array_slice($array,$offset,$length);

$offset: if the offset is non-negative then the sequence will start from that number but if it negative then the sequence will start that far from the end of the array.

$length: $length specifies that the sequence will contain that many elements. If it is negative then the sequence will have that many elements from the end of the array. If it is not specified then the sequence will have everything starting from offset to the end of the array.

example :

<?php
$flowers=array("Rose","Lily","Lotus","Sunflower");
array_slice($flowers,1);       // returns "Lily","Lotus" and "Sunflower"
array_slice($flowers,-1,2);    // returns "Sunflower" and "Lotus"
array_slice($flowers,-3);      // returns "Lily" and "Rose"
?>

6) array_splice() function: array_splice function is used to remove a portion of the array and replace it with something else. Removal of elements depends on offset and length parameters.

$offset: If the offset value is positive then the start of the removed part is from the offset you specified. If it is negative then starts that far from the end of the array.

$length: If length is positive then many elements are removed from the array. If it is negative then many elements are removed from the end of the array. If it is not specified then it will remove all the elements starting from offset to the end of the array.

example :


<?php
$flowers=array("Rose","Lily","Lotus","Sunflower");
array_splice($flowers,1);       // $flowers will now be "Rose"
array_splice($flowers,1,-1);    // $flowers will now have "Rose" and "Lotus"
array_splice($flowers,-1,1,array("Jasmine"));    // $flowers will now have "Rose","Lily","Lotus" and "Jasmine"
?>


7) array_unique() function : array_unique function is used to find the distinct element of an array.

example : 

<?php
$names=array("Matt","hardy","hardy","Matt","Matt");
array_unique($names);    // returns "Matt","hardy"
?>

8) array_reverse() function : array_reverse function reverses the element of an array

example:

<?php
$flowers=array("Rose","Lily","Lotus");
array_reverse($flowers);    // returns "Lotus","Lily","Rose"
?>

9) sort() function: sort function is used to sort the elements of the array. If words are present then it will sort them in alphabetical order

example :


<?php
$flowers=array("Rose","Lily","Jasmine");
sort($flowers);    // returns "Jasmine","Lily","Rose"
?>

Also, see 5 Basic Interview questions in PHP you should know


Comments

Popular posts from this blog

Tricky Questions or Puzzles in C

Program to uncompress a string ie a2b3c4 to aabbbcccc

Number series arrangement puzzles