Array Questions in PHP interview

PHP Array Interview Questions

What is PHP Array?

PHP array is a special type of variable that can hold multiple values in one single variable. There are 3 different types of array available in PHP Programming Language they are Indexed arrays, Associative arrays, and Multidimensional arrays.

Here is the list of top PHP array interview questions that can be asked by the Interviewer in PHP Interviews to Freshers and Experience.

Download PHP Array Interview Questions PDF

Below are the list of Best PHP Array Interview Questions and Answers

PHP Array is a special type of variable that stores multiple values in a single variable. There are 3 types of array available in PHP they are

  • Indexed arrays
  • Associative arrays
  • Multidimensional arrays

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

$array1=array() or $array1=[];

You can use count() function to count the number of elements in a PHP Array.

$array=['ram','ravi','ajay','vijay'];
echo count($array); // outputs 4

PHP array_key_exists() Function is used to check a key exist or not in an array.

Example Uses

<?php
$a=array("fruit"=>"Apple","Car"=>"BMW");
if (array_key_exists("Apple",$a))
  {
  echo "Array Key exists!";
  }
else
  {
  echo "Array Key does not exist!";
  }
?>

PHP explode() function is used to create an array from a string in PHP. Explode function breaks a string using a delimiter into an array.

Below is the basic syntax to convert a string to an array in PHP

<?php
$string = "This is a string in PHP";
$php_array= explode(" ",$string);
print_r(" ",$php_array);
?>

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

There are basically three types of arrays available in PHP. They are:

  • Indexed arrays
  • Associative arrays
  • Multidimensional arrays

PHP functions for Sorting Arrays are

  • sort(): Used for sorting arrays in ascending order.
  • rsort(): used for sorting arrays in descending order
  • asort(): used to sort associative arrays in ascending order, by value
  • ksort(): used to sort associative arrays in ascending order, by key
  • arsort(): used to sort associative arrays in descending order, by value
  • krsort(): used sort associative arrays in descending order, by key

json_encode() function is used to convert a JSON string to an array in PHP.

Syntax

json_decode(string, assoc, depth, options)

array_rand() function is used to get random value from PHP array.

Usage

<?php
$a=array("apple","banana","grapes","mango","orange");
echo $random_value=array_rand($a);
?>

Array PUSH and POP both are inbuilt PHP functions and used to work with an array.

Array push is used to add one or more elements onto the end of an array

Example

<?php
$a=array("car","truck");
array_push($a,"van","plane");
print_r($a);
?>

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

Syntax

<?php
$stack = array("orange", "banana", "apple", "raspberry");
echo $fruit = array_pop($stack); // prints raspberry

?>