PHP Array Functions

Last updated on Mar 10, 2024
  • Share
PHP Arrays

PHP array functions are regular among the very important ones that interviewers select for candidates. For those who are unaware of PHP array, this is a data structure that allows developers to store multiple elements with similar data type under a single variable, which save developers the effort to create a different variable for every data. The PHP arrays functions are extremely helpful creating a list of elements of similar types, which developers will able to access using their key or index.

Types of arrays in PHP;
  • Numeric or Indexed Arrays,
  • Multidimensional Arrays
  • Associative Arrays

Most Frequently Asked PHP Array Functions

Here in this article, we will be listing frequently asked PHP Arrays and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

Q11. What is the difference between associative array and indexed array?
Answer
Associative Arrays Indexed or Numeric Arrays
This is a type of arrays which used named specific keys to assign and store values in the database. This type of arrays store and assign values in a numeric fashion with count starting from zero.
Example
Example of an associative array:

$name_two["i am"] = "zara";
$name_two["anthony is"] = "Hello";
$name_two["ram is"] = "Ananya";
echo "Accessing elements directly:\n";

echo $name_two["i am"], "\n";
echo $name_two["anthony is"], "\n";
echo $name_one["Ram is"], "\n";

Output:

Accessing elements directly:
zara
Hello
Ananya

Example of an indexed or numeric array:

$name_two[1] = "Sonu Singh";
echo "Accessing the array elements directly:\n";
echo $name_two[1], "\n";

Output:

Sonu Singh

Q12. How to display array structure and values in PHP?
Answer

You can either use the PHP var_dump() or print_r() function to check the structure and values of an array. The var_dump() method gives more information than print_r().

Example

$lists = array("Best", "Interview", "Questions");
// Print the array
print_r($lists);
var_dump($lists);

Q13. What is the use of is_array() and in_array()?
Answer

is_array() : It is an inbuilt function used in PHP. It is used to check whether a variable is an array or not.

in_array() : It is used to check whether a given value exists in an array or not. It returns TRUE if the value is exists in array, and returns FALSE otherwise.

Q14. What is implode() in php?
Answer
PHP implode() function is used join array elements with a string.In other words we can say it returns a string from the elements of an array.
Example

$array = array('My','Name','Is','BestInterViewQuestion');
echo implode(" ",$array)


// OUTPUT : My Name Is BestInterViewQuestion

Q15. What is explode() in php?
Answer

PHP explode() function is used to break a string into an array.

Example

$string = "My Name Is BestInterviewQuestion";
print_r (explode(" ",$string));

// OUTPUT : Array ( [0] => My [1] => Name [2] => Is [3] => BestInterviewQuestion )

Q16. What is the use of array_search() in php?
Answer

array_search() is a inbuilt function of PHP which is used to search a particular value in an array and if the value is found then it returns its corresponding its key.

Example

$array = array("1"=>"My", "2"=>"Name", "3"=>"is", "4"=>"BestInterviewQuestion");
echo array_search("BestInterviewQuestion",$array);

// OUTPUT 4

Q17. How to get elements in reverse order of an array in php?
Answer

For this we can use array_reverse() function.

Example

$array = array("1"=>"Best","2"=>"Interview","3"=>"Question");
print_r(array_reverse($array));

// OUTPUT Array ( [3] => Question [2] => Interview[1] => Best)

Q18. How do you remove duplicates from an array?
Answer

We can use the PHP array_unique() function to remove the duplicate vlaues form an array.

Example

$array = array("a"=>"best","b"=>"interviewquestion","c"=>"best");
print_r(array_unique($array));

// OUTPUT : Array ( [a] => best [b] => interviewquestion)

Q19. What is the different between count() and sizeof() in php?
Answer

Both are used to count elements in a array.sizeof() function is an alias of count() function used in PHP. count() function is faster and butter than sizeof().

Example

$array = array('1','2','3','4');
$size = count($array);

//OUTPUT : 4

Q20. How to check a variable is array or not in php?
Answer

We can check a variable with the help of is_array() function in PHP. It's return true if variable is an array and return false otherwise.

Example

$var = array('X','Y','Z');
if (is_array($var))
echo 'It is an array.';
else
echo 'It is not an array.';

 

Arrays help developers store multiple elements within a single variable that can be accessed using a key or an index. We can also combine it with for each statement to a quick loop through an array with the use of very little code. While web application development, we can store tabular data in a pair of nested arrays to achieve better performance and utility. PHP arrays functions also have various functions such as merge, sort, split and intersect which makes it easy for us to efficiently manipulate them. With the inefficiency of strong typing and OOP-object iteration support in PHP, arrays are the most preferable data structure to store and manipulate data. We will further continue here with top PHP Array interview questions picked by industry leaders with their suggested answers for your practice.

Reviewed and verified by Umesh Singh
Umesh Singh

My name is Umesh Singh having 11+ experience in Node.JS, React JS, Angular JS, Next JS, PHP, Laravel, WordPress, MySQL, Oracle, JavaScript, HTML and CSS etc. I have worked on around 30+ projects. I lo...