PHP Array Interview Questions For Freshers & Experienced

  • Question 1) How to declare an array in PHP?
  • Question 2) List different types of array available in PHP?
  • Question 3) What is use of array_chunk()?
  • Question 4) How to compare two array in PHP?
  • Question 5) How to get all keys of an array in PHP?
  • Question 6) Php array starts with what index?
  • Question 7) How to get all the values of an array?
  • Question 8) Which function is used to get number of Array elements?
  • Question 9) Enlist different PHP functions available to sort an array?
  • Question 10) What is the use of in_array() function in PHP?
  • Question 11) How to convert a JSON string to an array in PHP?
  • Question 12) Explain when we use of array_search() in php?
  • Question 13) What is use of array filter in PHP?
  • Sharad Jaiswal
  • 31st Dec, 2018
  • 719 Followers

PHP Array Interview Questions
Updated ON

31st Dec, 2018

No. of Questions

15

Posted In

PHP

Author

Sharad Jaiswal

Q1. How to declare an array in PHP?

An array is a variable that can hold multiple values of the same type. In PHP, the array is created using the array() construct. This function takes any number of key-value pairs as arguments that are separated by a comma. By default, you can only give the value in the array function and the key will be the index number.

//syntax for array declaration
array(key1=>value1, key2=>value2, key3=>value3, …);
//example <? $arrayExample = array(“car1”=>”BMW”, “car2”=>”audi”, “car3”=>”Tesla”); ?>

From PHP 5.4, the array() can be replaced with []

//example array from PHP 5.4
<?
  $arrayExample = [“car1”=>”BMW”, “car2”=>”audi”, “car3”=>”Tesla”];
?>

Q2. List different types of array available in PHP?

In PHP, the arrays are divided into three types.

Indexed array – These arrays have values with a numeric index. The values are stored and accessed using the linear order of the index number. The index value starts from one.

//example
<?php
$linearArray = array(1, 2, 3, 4, 5, 6);
//to access the array value
$var = $linearArray[0];
?>

Associative array – These arrays have key specified for every value to store and access instead of the index value. The associative array has the key value as a string.

//example
<?php
$associativeArray = array(“one”=>1, “two”=2, “three”=>3);
//to access the value
$var = $associativeArray[‘one’];
?>

Multidimensional Array – The elements in these arrays have an array as a sub-element and so on. Each element may be an array here. The values in these arrays are accessed using multiple indexes.

//example
<?php
$multidimensionalArray = array(“firstArray”=>array(1, 2, 3),
“secondArray”=>array(4, 5, 6),
“fourthArray”=>array(7, 8, 9));
//to access the values
$var = $multidimensionalArray[‘firstArray’][2]; //returns 3
?>

Q3. What is use of array_chunk()?

Depending on the parameters passed to the function, the array_chunk() splits the given array into chunks of the given size. It is an inbuilt function in PHP.

Array array_chunk($array, $size, $preserve_keys)

Here, the $array is the array that should be divided into chunks. $size is the integer parameter that defines the size that the array should be divided. $preserve_keys is the Boolean parameter to preserve the key when it is set to TRUE. A multidimensional array whose index starts from 0 is returned from this function.

//example
<?php
$array = array(1, 2, 3, 4)
array_chunk($array, 2);
?>

The array_chunk function returns, Array([0]=>Array([0]=>1, [1]=>2), [1]=>Array([0]=>3, [1]=>4))

Q4. How to compare two array in PHP?

The array_diff() function in the PHP compares two or more arrays and returns the difference. The difference is the values that are present in the first array and not in the rest.

//syntax
array_diff(array1, array2, array3, …)

//example
<?php
$array1 = array(1, 2, 3)
$array2 = array(2, 3)
print_r(array_diff($array1, $array2))
?>

The above program returns, Array(1) as it is the difference.

Q5. How to get all keys of an array in PHP?

The array_keys() is an inbuilt function in the PHP that returns all the keys or only a subset of keys of the array that is passed.

//syntax
array_keys(input_array, search_key_value, strict) 

Here the input_array is the specified array whose keys should be returned, search_key_value is the value to be checked, and strict is an optional parameter to determine is strict comparison should be used or not.

//example
<?php
$array = array(“c1”=>”india”, “c2”=>”nepal”, “c3”=>”myanmar”);
Print_r(array_keys($array));
?>

The above program returns the arrays containing the keys with the numbered index.

That is, Array([0]=>c1, [1]=>c2, [2]=>c3)

Q6. Php array starts with what index?

By default, all the arrays in the PHP start with the index value of 0.

Q7. How to get all the values of an array?

The array_values() function in the PHP is used to return all the values of the array starting from index value 0 and increasing by 1. It doesn't return the key value.

//syntax
array_values($array)

//example
<?php
$array = array("car", "bus", "train");
print_r($array);
?>

The above example returns Array([0]=>car [1]=>bus [2]=>train)

Q8. Which function is used to get number of Array elements?

The PHP count() function is used to return the size or the number of elements in the array.

//syntax
count(array, mode)

Here the array parameter is the array whose size is to be returned and mode is an optional Boolean value specifies whether to count the values in the multidimensional array or not. By default, it is 0 and does not count.

//example
<?php
$array = array(“one”, “two”, “three”);
print_r(array);
?>

The above program returns value 3.

Q9. Enlist different PHP functions available to sort an array?

Here are the different PHP functions to sort an array,

  • sort() – It arranges the array in ascending order.
  • rsort() – It arranges the array in descending order.
  • asort() – It arranges the associative array according to their values in the ascending order.
  • ksort() – It arranges the associative array according to their keys in the ascending order.
  • arsort() – It arranges the associative array according to their values in the descending order.
  • krsort() – It arranges the associative array according to their keys in the descending order.

Q10. What is the use of in_array() function in PHP?

The in_array() is a built-in function that checks whether the specified value is present in the array or not. It returns TRUE if it is found or FALSE otherwise.

//syntax
bool in_array ( $val, $array_name ,$mode )

$val is the parameter to be searched and It can be of any type. $array_name is the array where the element must be searched. $mode is an optional Boolean parameter to specify the search type.

//example
<?php
$array = array(1, 2, 3, 4);
print_r(in_array(0, $array));
?>

The above program FALSE as 0 is not present in the $array.

Q11. How to convert a JSON string to an array in PHP?

The json_decode() function is used to convert the JSON data into an object or associative array.

//syntax
json_decode(string, assoc, depth, options)

Here the string variable specifies the JSON value to be decoded. Assoc is an optional Boolean variable. If it is set to true, it will return the JSON data as an associative array or PHP object otherwise. Depth is an optional variable that specifies the recursion depth.

//example
<?php
$json =‘{“value”:25}’;
json_decode($json, true);
?>

The above program returns an associated array, array(1){[“value”]=>int(25)}

Q12. Explain when we use of array_search() in php?

The array_search is an inbuilt function in PHP that is used to search a specified in the array. It returns the key of the value if the value is found or it returns FALSE otherwise. It returns the key of the first matching value if multiple values in the array match the specified value.

//syntax
array_search($value, $array, strict_parameter)

Here the $value is the parameter that is to be searched in the specified $array. Strict_parameter is an optional parameter that specifies the strictness of the search.

//example
<?php
$array = array(“one”, “two”, “three”);
array_search(“two”, $array);
?>

The program returns the value 1 as the index is the key in Indexed array.

Q13. What is use of array filter in PHP?

The inbuilt array_filter function is used to filter the array based on the user-defined callback function. The callback function is passed over each value in the array for filtering elements. If the function returns true, then the value is returned to the resultant array or not otherwise.

//syntax
array array_filter($array, $callback_function, $flag)

Here the $array is the specified array for filtering. $callback_function is the function that is iterated over the array. $flag is an optional value that specifies how the values should be passed to the callback function.

//example
<?php
function odd($array){
{  
   if($array%2!=0)
      return TRUE;
   else
      return FALSE; 
}
$array = array(1, 2, 3, 4, 5);
print_r(array_filter($aarray, “odd”));
?>

The above program returns an array of odd values only, Array([0]=>1[1]=>3[2]=>5)

Q14. How to insert an element in PHP array?

To insert an element in PHP, you can use the array_splice() function. By default, it removes the selected elements in the specified array and replaces it with new elements. You can use it to insert new elements if you specify the length parameter in the array_splice() function as 0 as it doesn’t remove array elements. The start parameter specifies where to insert the new element in the array.

//example
<?php
$array1 = array(1, 2);
$array2 = array(3, 4);
array_splice($array1, 2, 0, $array); //here, 2 specifies the index value to insert new element.
?>

The above program returns a new array with the inserted element, array(1, 2, 3, 4).

Q15. Which function is used to add value to the end of PHP array?

The array_push() function in PHP is used to insert one or more values at the end of an array.

//syntax
array_push(array, value1, value2, ...)

//example
<?php
$array = array(1, 2, 3);
print_r(array_push($array, 4, 5));
?>

The above program prints Array(1, 2, 3, 4, 5).

About Author :

  • Author of PHP Array Interview Questions

    Sharad Jaiswal

    Sharad Jaiswal is Sr. Web Developer from Noida area. He have rich experience in PHP, Angular Js, React, javascript and Node .If you any query or project on these programming you can drop your enquiry in comment section.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.