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.
PHP array is a special type of variable that stores multiple values in a single variable. There are three array types available 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);
?>
Using count() function you can use the number of element in array
$array=['Developer','Diary','ABC','Example'];
echo count($array);
// outputs 4
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.
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!";
}
?>
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 )
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 )
Following types of sort function available in array
using json_encode() function we can convert a JSON string to an array 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.
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 )
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]]
?>
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
?>
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
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.
Introduction Git tags are an essential feature of version control systems, offering a simple way…
Introduction The methods that browsers employ to store data on a user's device are referred…
Introduction A well-known open-source VPN technology, OpenVPN provides strong protection for both people and businesses.…
Introduction Integrating Sentry into a Node.js, Express.js, and MongoDB backend project significantly enhances error tracking…
Introduction In the world of JavaScript development, efficiently managing asynchronous operations is essential. Asynchronous programming…
Introduction Let's Encrypt is a Certificate Authority (CA) that makes it simple to obtain and…