Skip to main content

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 ( Updated for 2026)

Updated for 2026 This article was originally written when C/C++ puzzles were commonly asked in interviews. While such language-specific puzzles are less frequent today, the problem-solving and logical reasoning skills tested here remain highly relevant for modern Software Engineering, Data Engineering, SQL, and system design interviews . Why These Puzzles Still Matter in 2026 Although most Software &   Data Engineering interviews today focus on Programming, SQL, data pipelines, cloud platforms, and system design , interviewers still care deeply about how you think . These puzzles test: Logical reasoning Edge-case handling Understanding of execution flow Ability to reason under pressure The language may change , but the thinking patterns do not . How These Skills Apply to Data Engineering Interviews The same skills tested by C/C++ puzzles appear in modern interviews as: SQL edge cases and NULL handling Data pipeline failure scenarios Incremental vs ...

Program to uncompress a string ie a2b3c4 to aabbbcccc

Below is the program to uncompress a string #include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { char str[100]="a2b3c4d8u7"; for(int i=0;str[i]!='\0';i++) { if(i%2!=0) { for(int j=0;j<atoi(&str[i]);j++) { printf("%c",str[i-1]); } } } getch(); } Want to become a Data Engineer? Check out below blog posts  1.  5 Key Skills Every Data Engineer needs in 2023 2.  How to prepare for Data Engineering Interviews 3.  Top 25 Data Engineer Questions

Programs and Puzzles in technical interviews i faced

I have attended interview of nearly 10 companies in my campus placements and sharing their experiences with you,though i did not got selected in any of the companies but i had great experience facing their interviews and it might help you as well in preparation of interviews.Here are some of the puzzles and programs asked to me in interview in some of the good companies. 1) SAP Labs I attended sap lab online test in my college through campus placements.It had 3 sections,the first one is usual aptitude questions which i would say were little tricky to solve.The second section was Programming test in which you were provided snippet of code and you have to complete the code (See Tricky Code Snippets  ).The code are from different data structures like Binary Tree, AVL Tree etc.Then the third section had questions from Database,OS and Networks.After 2-3 hours we got the result and i was shortlisted for the nest round of interviews scheduled next day.Then the next day we had PPT of t...