Loading…

PHP Arrays

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.

PHP offers three types of arrays;
  • Numeric or Indexed Arrays,
  • Multidimensional Arrays
  • Associative Arrays

Arrays help developers store multiple elements within a single variable which can be accessed using a key or an index. We can also combine it with foreach statement to 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.

Last update: 12 Sep 2019, 25 Questions and Answers

Read PHP Arrays Below

Looking for a new job? Do not miss to read our PHP Arrays and answers. Whether you are a fresher or an experienced, these questions and answers that can help you to crack your interview.

PHP supports three types of arrays:-

  • Indexed Array
  • Associative array
  • Multi-dimensional Array
44 7

array_pop() : It is used to delete or remove the last element of an array.

$a=array("blue","black","skyblue");
array_pop($a);

OUTPUT : Array ( [0] => blue[1] => black)

array_push() : It is used to Insert one or more elements to the end of an array.

$a=array("apple","banana"); array_push($a,"mango","pineapple");

OUTPUT : Array ( [0] => apple[1] => banana[2] => mango[3] => pineapple)

14 1

array_combine() : It is used to create a new array by using the key of one array as keys and using the value of another array as values. The most important thing is using array_combine() that, number of values in both arrays should be same.

array_merge() : It merges one or more than one array such that the value of one array appended at the end of first array and if the arrays have same strings key then the later value overrides the previous value for that key .

6 1

We can use the count() or sizeof() function to get the number of elements in an array.

Example

                                                    

$array1 = array("1","4","3");
echo count($array1);

OUTPUT : 3

4 0

$originalArray = array( 'ram', 'sita', 'luxman', 'hanuman', 'ravan' );
$newArray = array( 'kansh' );
array_splice( $originalArray, 3, 0, $newArray);

// OUTPUT is ram sita luxman hanuman kansh ravan

9 8

To check key in array, we can use array_key_exists().

$item=array("name"=>"umesh","class"=>"mca");
if (array_key_exists("name",$item))
{
echo "Key is exists";
}
else
{
echo "Key does not exist!";
}

4 0

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.

2 0
  • sort() - It is used to sort an array in ascending order
  • rsort() - It is used to sort an array in descending order
  • asort() - It is used to sort an associative array in ascending order, according to the value
  • ksort() - It is used to sort an associative array in ascending order, according to the key
  • arsort() - It is used to sort an associative array in descending order, according to the value
  • krsort() - It is used to sort an associative array in descending order, according to the key
3 1

To create an array, we can use the array() construct in PHP.

Example

                                                    

Syntax : $array = array( values );

Example : $directors = array( "1", "7", "17", "23" );

3 2
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

0 0
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 )

1 0

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

2 0

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)

1 0

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)

1 0

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

3 0

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.';

 

0 0

It is an inbuilt function in PHP. It is one of the most simple functions that is used to count all the values inside an array. In other words we can say that it is used to calculate the frequency of all of the elements of an array.

Example

                                                    

$array = array("B","Cat","Dog","B","Dog","Dog","Cat");
print_r(array_count_values($array));

// OUTPUT : Array ( [B] => 2 [Cat] => 2 [Dog] => 3 )

8 0

array_key_exists() : It is used to checks an array for a particular key and returns TRUE if the key exists and FALSE if the key does not exist.

array_keys() : This function returns an array containing the keys. It takes three parameters out of which one is mandatory and other two are optional.
Syntax : array_keys(array,value,strict)

2 0

You can use rsort() to get the highest value in the array.

Example

                                                    

$array = array('5','2','8','4');
$result = rsort($array);


//OUTPUT : 8

1 4
Ask Question