Skip to content

PHP Array Interview Questions and Answers

php array interview

Here you can read interview questions and answers related to PHP arrays. This list contains popular and frequently asked interview questions about PHP array functions. Prepare for jobs with our list of PHP FAQs.

Top Array Questions in PHP interviews

  • What is an array in PHP?
  • How to create an empty array in PHP?
  • How to get number of elements in an Array?
  • What is difference between array_merge and array_combine?
  • How to check a key exist in an array?
  • How to create an array from PHP string?
  • How to Delete an element from an array?
  • List functions available to sort an php array?
  • How to convert a JSON string to an array in PHP?
  • What is difference between count or sizeof function in php?
  • How to remove duplicate values from PHP Array?
  • How to get a random value from a PHP array?
  • What is difference between array PUSH and POP?
Q. What is an array in PHP?

PHP array is a special type of variable that stores multiple values ​​in a single variable. There are three array types available in PHP:

  • Indexed array
  • Associative array
  • Multidimensional array
Q. How to create an empty array in PHP?

It’s very easy to create an array in PHP. You can create an array in PHP using the array() function or simply assigning [] in front of the variable.

Syntax of empty array

$exampleArray = []; 
$exampleArray = array();
$exampleArray = (array) null;

Basic example of empty array

<?php

// Create an empty array
$exampleArray = array();
// Push elements to the array
array_push($exampleArray, "developer", "diary");
// Display array elements
print_r($emptyArray);
?>
Q. How to get number of elements in an Array?

Using count() function you can use the number of element in array

$array=['Developer','Diary','ABC','Example'];
echo count($array); 
// outputs 4
Q. What is difference between array_merge and array_combine?

array_combine() is used to create a new array using the key of one array as keys and the value of another array as values.

While array_merge() merges one or more arrays so that the value of an array is appended to the end of the first array.

Difference Between Array_merge() And Array_combine() In PHP

Q. How to check a key exist in an array?

The PHP function array_key_exists() is used to check whether a key exists in an array or not.

Example

<?php
$a = array("fruit"=>"Mango","Car"=>"Maruti");
if (array_key_exists("fruit",$a)) {
  echo "Array Key exists!";
}else{
  echo "Array Key does not exist!";
}
?>
Q. How to create an array from PHP string?

Using explode() php built in function we can create array from string.

Below is the basic example

<?php
$string = "PHP String to Array Example";
$php_array= explode(" ",$string);
print_r($php_array);
?>
// Output
Array ( [0] => PHP [1] => String [2] => to [3] => Array [4] => Example )
Q. How to Delete an element from an array?

You can use the array_splice() or unset() function to remove an element from the PHP array.

unset() Example

<?php
$array1 = array(1, 5, 3, 4, 9, 11);
unset($array1[3]); // Unset the element
echo "Array elements after unset:<br>";
print_r($array1);
?>
// Output
Array elements after unset:
Array ( [0] => 1 [1] => 5 [2] => 3 [4] => 9 [5] => 11 )

array_splice() example

<?php
$arr = array(1, 5, 3, 4, 9, 11);
array_splice($arr,3,1);
print_r($arr);
?>
// Output
Array ( [0] => 1 [1] => 5 [2] => 3 [3] => 9 [4] => 11 )
Q. List functions available to sort an php array?

Following types of sort function available in array

  • sort(): sorting array in ascending order.
  • rsort(): sorting array in descending order
  • asort(): sort associative array in ascending order, by value
  • ksort(): sort associative array in ascending order, by key
  • arsort(): sort associative array in descending order, by value
  • krsort(): sort associative array in descending order, by key
Q. How to convert a JSON string to an array in PHP?

using json_encode() function we can convert a JSON string to an array in PHP.

Q. What is difference between count or sizeof function in php?

According to the official PHP doc, there is no difference between the count() or sizeof() function in PHP, which means that both are the same, which means that the sizeof() function is just the alias of the function count is, i.e. size() uses the same as the count function below.

Q. How to remove duplicate values from PHP Array?

You can use array_unique() function to remove duplicate values ​​from PHP array. This means that if two or more array values ​​are the same, the first appearance is kept and the other is discarded.

array_unique() Example

<?php
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
?>
// Output
Array ( [a] => red [b] => green )
Q. How to get a random value from a PHP array?

Using array_rand() function we can get random value from PHP array.

Example : Following example every refresh you will get different value

<?php
$a=array("red","green","blue","yellow","brown");
$random_keys=array_rand($a,3);
echo $a[$random_keys[0]]
?>
Q. What is difference between array PUSH and POP?

Array PUSH and POP are PHP built-in functions and are used to work with an array.

Array push is used to add elements to the end of an array

Example

<?php
$a=array("A","B");
array_push($a,"C","D");
print_r($a);
?>
// Output
Array ( [0] => A [1] => B [2] => C [3] => D )

Array pop removes and returns the value of the last element of an array.

Example

<?php
 $stack = array("A", "B", "C", "D");
 echo $result = array_pop($stack); // prints D
?>

More interview Questions

PHP OOPS Interview Questions And Answers (2022)

PHP Interview Questions And Answers

Node.Js Interview Questions And Answers

CodeIgniter Interview Questions And Answers

JavaScript Interview Questions And Answers

Latest MySQL Interview Questions And Answers

Laravel Interview Questions And Answers

Conclusion

In this article we tried to understand top php array interview questions. Hope this will help you to learn and clear php interview. Also I drop link for other interview questions so can track the same.