PHP Array Functions

Last updated on Mar 10, 2024
  • Share
PHP Arrays

PHP array functions are regular among the very important ones that interviewers select for candidates. For those who are unaware of PHP array, this is a data structure that allows developers to store multiple elements with similar data type under a single variable, which save developers the effort to create a different variable for every data. The PHP arrays functions are extremely helpful creating a list of elements of similar types, which developers will able to access using their key or index.

Types of arrays in PHP;
  • Numeric or Indexed Arrays,
  • Multidimensional Arrays
  • Associative Arrays

Most Frequently Asked PHP Array Functions

Here in this article, we will be listing frequently asked PHP Arrays and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

Q1. What is array filter in PHP?
Answer

The array_filter() method filters the values of an array the usage of a callback function. This method passes each value of the input array to the callback function. If this callback function returns true then the current value from the input is returned into the result array.

Syntax:

array_filter(array, callbackfunction, flag)

  • array(Required)
  • callbackfunction(Optional)
  • flag(Optional)
Example

function test_odd_number(int $var)
{
   return($var & 1);
}
$array=array(1,3,2,3,4,5,6);
print_r(array_filter($array,"test_odd_number"));

Q2. How to get first element of array in php?
Answer

There are various methods in PHP to get the first element of an array. Some of the techniques are the use of reset function, array_slice function, array_reverse, array_values, foreach loop, etc.

Example

Suppose we have an array like
$arrayVar = array('best', 'interview', 'question', 'com');

1. With direct accessing the 0th index:
echo $arrayVar[0];

2. With the help of reset()
echo reset($arrayVar);

3. With the help of foreach loop
foreach($arrayVar as $val) {
    echo $val;
    break; // exit from loop
}

Q3. How many types of array supported in php?
Answer

PHP supports three types of arrays:-

  • Indexed Array
  • Associative array
  • Multi-dimensional Array
Q4. How to use array_pop() and array_push() in PHP?
Answer

1. array_pop()

It is used to delete or remove the last element of an array.

Example

$a=array("blue","black","skyblue");
array_pop($a);

OUTPUT : Array ( [0] => blue[1] => black)

2. array_push()

It is used to Insert one or more elements to the end of an array.

Example

$a=array("apple","banana"); array_push($a,"mango","pineapple");

OUTPUT : Array ( [0] => apple[1] => banana[2] => mango[3] => pineapple)

Also Read: PHP Strings
Q5. How to use array_merge() and array_combine() in PHP?
Answer
array_combine()

array_combine() : It is used to create a new array by using the key of one array as keys and using the value of another array as values. The most important thing is using array_combine() that, number of values in both arrays should be same.

$name = array("best","interview","question");
$index = array("1","2","3");
$result = array_combine($name,$index);

array_merge()

array_merge() : It merges one or more than one array such that the value of one array appended at the end of first array and if the arrays have same strings key then the later value overrides the previous value for that key .

$name = array("best","interview","question");
$index = array("1","2","3");
$result = array_merge($name,$index);

Q6. What is the use of array_count_values() in php?
Answer

It is an inbuilt function in PHP. It is one of the most simple functions that is used to count all the values inside an array. In other words we can say that it is used to calculate the frequency of all of the elements of an array.

Example

$array = array("B","Cat","Dog","B","Dog","Dog","Cat");
print_r(array_count_values($array));

// OUTPUT : Array ( [B] => 2 [Cat] => 2 [Dog] => 3 )

Q7. How to get total number of elements used in array?
Answer

We can use the count() or sizeof() function to get the number of elements in an array.

Example

$array1 = array("1","4","3");
echo count($array1);

OUTPUT : 3

Q8. How to get specific key value from array in php?
Answer

To check key in the array, we can use array_key_exists().

$item=array("name"=>"umesh","class"=>"mca");
if (array_key_exists("name",$item))
{
   echo "Key is exists";
}
else
{
   echo "Key does not exist!";
}

 

 

Q9. How to insert an new array element in array?
Answer

$originalArray = array( 'ram', 'sita', 'luxman', 'hanuman', 'ravan' );
$newArray = array( 'kansh' );
array_splice( $originalArray, 3, 0, $newArray);

// OUTPUT is ram sita luxman hanuman kansh ravan

Q10. Explain different sorting function in PHP?
Answer
  • sort() - It is used to sort an array in ascending order
  • rsort() - It is used to sort an array in descending order
  • asort() - It is used to sort an associative array in ascending order, according to the value
  • ksort() - It is used to sort an associative array in ascending order, according to the key
  • arsort() - It is used to sort an associative array in descending order, according to the value
  • krsort() - It is used to sort an associative array in descending order, according to the key

Arrays help developers store multiple elements within a single variable that can be accessed using a key or an index. We can also combine it with for each statement to a quick loop through an array with the use of very little code. While web application development, we can store tabular data in a pair of nested arrays to achieve better performance and utility. PHP arrays functions also have various functions such as merge, sort, split and intersect which makes it easy for us to efficiently manipulate them. With the inefficiency of strong typing and OOP-object iteration support in PHP, arrays are the most preferable data structure to store and manipulate data. We will further continue here with top PHP Array interview questions picked by industry leaders with their suggested answers for your practice.

Reviewed and verified by Umesh Singh
Umesh Singh

My name is Umesh Singh having 11+ experience in Node.JS, React JS, Angular JS, Next JS, PHP, Laravel, WordPress, MySQL, Oracle, JavaScript, HTML and CSS etc. I have worked on around 30+ projects. I lo...