PHP Arrays and answers
- How many types of array supported in php?
- What is the difference between array_pop() and array_push()?
- Explain the difference between array_merge() and array_combine()?
- How to get total number of elements used in array?
- How to insert an new array element in array?
- How to check an key is exists in array?
- What is the use of is_array() and in_array()?
- Explain different sorting function in PHP?
- How to create an array in php?
- What is implode() in php?
- What is explode() in php?
- What is the use of array_search() in php?
- How to get elements in reverse order of an array in php?
- How to remove all duplicate values in array in php?
- What is the different between count() and sizeof() in php?
- How to check a variable is array or not in php?
- What is the use of array_count_values() in php?
- What are the difference between array_keys() and array_key_exists() in php?
- What is the difference between array_merge() and array_merge_recursive() in php?
- Write a PHP script to get the largest key in an array?
- How do you remove duplicates from an array?
- What is array filter in PHP?
- What is the difference between associative array and indexed array?
- Which array function checks if the particular key exists in the array?
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.
array_pop() : It is used to delete or remove the last element of an array.
Array (  => blue => black)
array_push() : It is used to Insert one or more elements to the end of an array.
Array (  => apple => banana => mango => pineapple)
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 .
We can use the count() or sizeof() function to get the number of elements in an array.
$array1 = array("1","4","3");
OUTPUT : 3
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.
- 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
$array = array('My','Name','Is','BestInterViewQuestion');
echo implode(" ",$array)
// OUTPUT : My Name Is BestInterViewQuestion
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.
$array = array("1"=>"My", "2"=>"Name", "3"=>"is", "4"=>"BestInterviewQuestion");
// OUTPUT 4
For this we can use array_reverse() function.
$array = array("1"=>"Best","2"=>"Interview","3"=>"Question");
// OUTPUT Array (  => Question  => Interview => Best)
We can use the PHP
array_unique() function to remove the duplicate vlaues form an array.
$array = array("a"=>"best","b"=>"interviewquestion","c"=>"best");
// OUTPUT : Array ( [a] =>
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().
$array = array('1','2','3','4');
$size = count($array);
//OUTPUT : 4
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.
$var = array('X','Y','Z');
echo 'It is an array.';
echo 'It is not an array.';
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.
$array = array("B","Cat","Dog","B","Dog","Dog","Cat");
// OUTPUT : Array ( [B] => 2 [Cat] => 2 [Dog] => 3 )
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.
You can use
rsort() to get the highest value in the array.
$array = array('5','2','8','4');
$result = rsort($array);
//OUTPUT : 8