PHP Array Interview Questions and Answers

List of PHP Array Interview Questions and Answers. we will discuss various interview questions on PHP array that help you to prepare PHP interview.

 

List of PHP Array Interview Questions

  1. What is array in PHP?
  2. How to declare PHP array?
  3. How to check whether variable is an array or not in PHP?
  4. How to display or print the elements of PHP Array?
  5. How to insert value in PHP array?
  6. How to call PHP array?
  7. Can we assign PHP array to some other variable?
  8. how to insert elements at specific location?
  9. How to remove the elements from PHP array?
  10. How to remove the elements from PHP array from specific location?
  11. How to remove the first element of PHP array?
  12. How to remove the last element of PHP array?
  13. Can PHP array can have duplicate elements?
  14. How to sort the elements of PHP array?
  15. What are indexed array in PHP?
  16. What are PHP Associative array?
  17. How to extract the associative array element in PHP?
  18. How to get the size of the PHP Array?
  19. What is the use of array_pop() function?
  20. What is the purpose of array_push() function in PHP ?
  21. What array_slice() function do in PHP?
  22. How to check whether element exist in PHP array or not?
  23. How many types of array supported by PHP?
  24. How to get the sum of elements of PHP array?
  25. How to remove the duplicate values from PHP array?
  26. How to count the elements in a PHP array?
  27. How to get the current element in a PHP array?
  28. Can we insert null in PHP array?
  29. How to merge two array in PHP?
  30. How to add elements in PHP multidimensional array?
  31. How to traverse PHP array?
  32. How to sort PHP array in ascending or descending order?
  33. How to delete all the elements in PHP array?
  34. What is the purpose of implode function in PHP?
  35. What is the use of explode function in PHP?
  36. What is the purpose of sizeOf() in PHP array?

 

What is array in PHP?

Array is the one of the datatype in PHP. Arrays are used to store the data of same datatype. data is stored at specific location in array called index. items are called elements in Array.

In PHP Programming language, we can do everything with the Arrays as we can do in other programming languages.

 

How to declare PHP array?

In PHP array is declared or created by <strong>array</strong> keyword.

  
<?php
$color = array("Red", "Yellow", "Blue");
?>

In this example, array of color is created and assigned to $color variable.

 

How to check whether variable is an array or not in PHP?

is_array(variable_name) is used to check whether variable is array or not. it will return 1 if variable is array


<?php
$color = array("Red", "Yellow", "Blue");

echo (is_array($color));
?> 

It will return 1 because color is an array in PHP.

 

How to display or print the elements of PHP Array?

PHP array elements can be printed by using <strong>print_r</strong> method instead of echo in PHP.

How to insert value in PHP array?

Values in PHP array can be inserted by couple of ways.

  • At the time of array creation : elements can be inserted at the time of array creation.

example: $color=array(“yellow”,”blue”);

  • array_push: elements in PHP array can be inserted by using array_push function.

example:


<?php
$color=array("green","white");
array_push($color,"red","pink");
print_r($color);
?> 

 

How to call PHP array?

It is very easy to call PHP array. Just write the array name.

 
<?php
$color=array("green","white"); 
array_push($color,"red","pink"); 
print_r($color); 
?>  
 

 

Can we assign PHP array to some other variable?

Yes, PHP array can be assigned to some other variable.

example

 

<?php
$color=array("green","white");
array_push($color,"red","pink");
//print_r($color);
$a=100;
$a=$color;
print_r($a);
?> 

Output

 Array ( [0] => green [1] => white [2] => red [3] => pink )

Here we assigned $color array to variable $a.

 

How to insert elements at specific location?

To insert element at specific location index is used.


<?php
$color=array("green","white");
array_push($color,"red","pink");
print_r($color);

$color[0]="yellow";
//echo "n";
print_r($color);
?>


 

output


Array ( [0] => green [1] => white [2] => red [3] => pink )

Array ( [0] => yellow [1] => white [2] => red [3] => pink )

 

above $color[0] ="yellow" is used to put the yellow at 0 index. hence value green is replaced with yellow value.

 

How to remove the elements from PHP array?

By using array_diff method we can remove the elements from the PHP array.


<?php
$color=array("green","white","red","pink");

$color=array_diff($color, ["red"]);

print_r($color);
?>

output

 Array ( [0] => green [1] => white [3] => pink ) &nbsp;

 

How to remove the elements from PHP array from specific location?

<strong>unset()</strong> function is used to remove the PHP array element from specific location.


<?php
$color=array("green","white","red","pink");

unset($color[0]);

print_r($color);
?>

output

&nbsp; Array ( [1] => white [2] => red [3] => pink )&nbsp; 

 

How to remove the first element of PHP array?

array_shift function is used to remove the first element from PHP array.


<?php
$color=array("green","white","red","pink");

array_shift($color);

print_r($color);
?>

Output

 Array ( [0] => white [1] => red [2] => pink ) 

 

How to remove the last element of PHP array?

array_pop is used to remove the last element from PHP array.


<?php
$color=array("green","white","red","pink");

array_pop($color);

print_r($color);
?>

output

 Array ( [0] => green [1] => white [2] => red )

 

Can PHP array can have duplicate elements?

Yes, PHP array can have duplicate values.


<?php
$color=array("green","white","red","pink","red");

print_r($color);
?>

output

 Array ( [0] => green [1] => white [2] => red [3] => pink [4] => red ) 

 

How to sort the elements of PHP array?

<strong>sort</strong> function is used to sorting the PHP array elements.


<?php
$color=array("green","white","red","pink","red");

print_r($color);

sort($color);

echo "after sorting";
print_r($color);

?>

output

&nbsp; Array ( [0] => green [1] => white [2] => red [3] => pink [4] => red )

after sorting

Array ( [0] => green [1] => pink [2] => red [3] => red [4] => white )

 

What are indexed array in PHP?

Index array is that array in which elements can be fetched or stored with index numbers. In PHP, by default array are indexed array.

example


<?php
$color=array("green","white","red","pink","red");

print_r($color);

?>

output

Array ( [0] => green [1] => white [2] => red [3] => pink [4] => red ) 

we can see that all the elements are stored by default with index values.

 

What are PHP associative array?

PHP associative array are those array which uses the names in keys instead of numeric numbers. We have seen PHP index array in which numeric values like 0,1,2.. etc are assigned in keys.

In PHP associative array, names are used instead of numbers.


<?php
$website=array("name" => "ByteArray.in", "topic" => "PHP");

print_r($website);

?>

 

output

 Array ( [name] => ByteArray.in [topic] => PHP ) 

 

How to extract the associative array element in PHP?

To get the value of the element in PHP associative array, just put the array name with the element name.


<?php
$website=array("name" => "ByteArray.in", "topic" => "PHP");

echo $website["name"];

?>

output

 ByteArray.in &nbsp;

 

 

How to get the size of the PHP Array

<strong>count</strong> or <strong>sizeOf</strong> are two functions which can be used to calculate the size of the PHP array.


<?php
$color=array("green","white","red","pink","red");
$fruits = array("banana","apple","orange");

echo count($color);
echo sizeOf($fruits);

?>

output


5

3

 

What is the use of array_pop() function in PHP?

<strong>array_pop</strong> function is used to remove the last element from PHP array.

 

What is the purpose of array_push function in PHP?

<strong>array_push</strong> function is used to insert the elements at the last position in PHP array.

 

What array_slice() function do in PHP?

The use of array_slice() function is to get the part of the PHP array. It returns the portion of the array.


<?php
$color=array("green","white","red","pink","red");

print_r(array_slice($color,2));

?>

output


Array ( [0] => red [1] => pink [2] => red )

 

How to check whether element exist in PHP array or not?

<strong>in_array()</strong> function is used to check whether any element exist in PHP array or not. It returns 1 if element exist.


<?php

$color=array("green","white","red","pink");

echo (in_array("green",$color));

?>

output

 1 

 

How many types of array supported by PHP?

There are 3 types of array supported by PHP.

  1. Associative Array – Associate array are those which having keys as named keys.
  2. Indexed Array – Indexed array are those which has key as numeric value
  3. Multidimensional Array –  Multidimensional array is that array which contain more than one array.

 

How to get the sum of elements of PHP array?

array_sum function is used to calculate the sum of elements of PHP array.


<?php

$marks=array(1,2,3,4,5);
echo (array_sum($marks));

?>

output

 15 

How to remove the duplicate values from PHP array?

PHP <strong>array_unique</strong> function is used to remove the duplicate elements from PHP array.


<?php

$color=array("green","white","red","pink","white","green");

print_r(array_unique($color));

?>

output


Array ( [0] => green [1] => white [2] => red [3] => pink )

 

How to count the elements in a PHP array?

<strong>count</strong> function of PHP is used to get the information of total elements in the PHP array.


<?php

$color=array("green","white","red","pink","white","green");

print_r(count($color));

?>

output

 6 

 

How to get the current element in a PHP array?

PHP current function is used to get the current value of PHP array.


<?php

$color=array("green","white","red","pink","white");

echo current($color);

?>

output

&nbsp; green 

green is the current element because color array is not in a traversing state.

 

Can we insert null in PHP array?

Yes, we can insert null in PHP array.


<?php

$color=array("green","white",null,"red","pink","white");

print_r ($color);

?>

output


Array ( [0] => green [1] => white [2] => [3] => red [4] => pink [5] => white )

 

How to merge two arrays in PHP?

By using <strong>array_merge</strong> function two or more arrays can be merged in PHP.


<?php

$color=array("green","white");
$color2=array("red","pink");
$all_color=array_merge($color,$color2);

print_r ($all_color);

?>

output


Array ( [0] => green [1] => white [2] => red [3] => pink )

 

How to add elements in PHP multidimensional array?

Elements can be added to PHP multidimensional array by using array_push function.


<?php
$all = array (
"number"=> array(1,2,3),
"name" => array("one","two","three")
);

array_push($all["name"],"four");
print_r ($all);



?>

output


Array ( [number] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [name] => Array ( [0] => one [1] => two [2] => three [3] => four ) )

In this example, we have added a value “four” in PHP multidimensional array.

 

How to traverse PHP array?

There are a couple of ways to traverse the PHP array. we will see foreach method to traverse PHP array.


<?php

$color=array("green","white");

foreach($color as $c) {

echo $c;
echo "\n";

}

//print_r ($all_color);

?>

output


green

white

 

How to sort PHP array in ascending or descending order?

sort – sort method is used to sort the PHP array in ascending order.

rsort – rsort method is used to sort the PHP array in descending order.

 

How to delete all the elements in PHP array?

All the elements in PHP array can be deleted by using the <strong>unset</strong> method.


<?php

$color=array("green","white");

unset($color);

print_r ($color);

?>

 

What is the purpose of implode function in PHP?

implode function is used to make the String out of PHP array elements with some concatenation.


<?php
$website = array("hello","ByteArray.in", "How","Are","You!");
echo implode(" ",$website);
?>

output


hello ByteArray.in How Are You!

 

What is the use of explode function in PHP?

Explode function works opposite to implode function. Purpose of explode function is to make the PHP array from string values.


<?php
$website = "hello ByteArray.in How Are You!";
print_r( explode(" ",$website));
?>

output


Array ( [0] => hello [1] => ByteArray.in [2] => How [3] => Are [4] => You! )

 

What is the purpose of sizeOf() in PHP array?

<strong>sizeOf</strong> method returns the number of elements in PHP array.


<?php
$website = array("hello","ByteArray.in", "How","Are","You!");
echo sizeOf($website);
?>

output

 5