MOST IMPORTANT QUESTIONS GENERALLY USED TO BE ASKED IN THE INTERVIEW ON ARRAY FUNCTIONS IN PHP
MOST IMPORTANT QUESTIONS GENERALLY USED TO BE ASKED IN THE INTERVIEW ON ARRAY FUNCTIONS IN PHP
Q1. What is Array Function()?
Ans: Array Function() can manipulate the array elements.
Q2. What is array_pop()? Explain with an example.
Ans: array_pop() is a manipulation function that removes the last element of an array.
CODE =>
<?php
$arr=["10","20","30"];
$arrpop=array_pop($arr);
print_r($arr);
?>
OUTPUT =>
Array ( [0] => 10 [1] => 20 )
Q3. What is array_push()? Explain with an example.
Ans: array_push() can insert the element at the end of the array.
CODE =>
<?php
$arr=["Tiger","Lion"];
$arrpush=array_push($arr, "Hyena");
print_r($arr);
?>
OUTPUT =>
Array ( [0] => Tiger [1] => Lion [2] => Hyena )
Q4. What is meant by array_shift()? Explain with an example.
Ans: array_shift() removes the first element of an array.
CODE =>
<?php
$arr=["Amit","Sanchari","Prosenjit","Anirban","Ayan","Amlan"];
$arrshift=array_shift($arr);
print_r($arr);
?>
OUTPUT =>
Array ( [0] => Sanchari [1] => Prosenjit [2] => Anirban [3] => Ayan [4] => Amlan )
Q5. What is meant by array_unshift()? Explain with an example.
Ans: array_unshift() inserts new elements at the beginning of an array.
CODE =>
<?php
$arr=["10","15","20"];
$arrunshift=array_unshift($arr, "5");
print_r($arr);
?>
OUTPUT =>
Array ( [0] => 5 [1] => 10 [2] => 15 [3] => 20 )
Q6. Why the array elements are not reversed while using "array_reverse($arr);"?
Ans: The array elements are not reversed while using array_reverse($arr) because after manipulation it is not able to store anywhere, but the array_reverse() function is working properly.
Q7. Why in other functions manipulation is working and output is generated while in "array_reverse($arr)" manipulation is not working and output is not reversed?
Ans: In other function manipulation is working and output is generated while in "array_reverse($arr)" manipulation is not working and output is not reversed because while adding the array elements either we are adding or deleting the array elements whereas in case of adding the array elements it is fixed while deleting the array elements we are terminating the array elements, so there is nothing to store anywhere but in case of array_reverse() function we need to store the array elements.
Q8. What is meant by array_keys()? Explain with an example.
Ans: array_keys() retruns an array containing the keys.
CODE =>
<?php
$arr=["Name"=>"Ayan Bhattacharjee","Age"=>"28+","DOB"=>"04/10/1997"];
$arrkeys=array_keys($arr);
print_r($arrkeys);
?>
OUTPUT =>
Array ( [0] => Name [1] => Age [2] => DOB )
Q9. What is meant by array_flip()? Explain with an example.
Ans: array_flip() is the swapping between key and value.
CODE =>
<?php
$arr=["Animal1"=>"Tiger","Animal2"=>"Lion"];
$arrflip=array_flip($arr);
print_r($arrflip);
?>
OUTPUT =>
Array ( [Tiger] => Animal1 [Lion] => Animal2 )
Q10. What is meant by array_diff()? Explain with an example.
Ans: array_diff() means diffrentiate. It helps to fetch different values.
CODE =>
<?php
$arr1=["Color1"=>"Yellow","Color2"=>"Orange","Color3"=>"Red"];;
$arr2=["Color1"=>"Yellow","Color2"=>"Green","Color3"=>"Red"];
$arrdiff=array_diff($arr1, $arr2);
print_r($arrdiff);
?>
OUTPUT =>
Array ( [Color2] => Orange )
Q11. What is meant by array_unique()? Explain with an example.
Ans: array_unique() removes the duplicate values from an array. If two or more array values are the same, the first array value will be kept and the duplicate values will be removed.
CODE =>
<?php
$arr=[2,2,2,4,6,6,6,6,6,8,10,10];
$arrunique=array_unique($arr);
print_r($arrunique);
?>
OUTPUT =>
Array ( [0] => 2 [3] => 4 [4] => 6 [9] => 8 [10] => 10 )
Q12. What is meant by array_sum()? Explain with an example.
Ans: array_sum() is the function which can addition or summation only the numeric values.
CODE =>
<?php
$arr=["I",100,"am",200,"going",300,"to",400,"Office",500,"TCS",600];
$arrsum=array_sum($arr);
print_r($arrsum);
?>
OUTPUT =>
2100
Q13. What is meant by array_combine()? Explain with an example.
Ans: array_combine() function combines two arrays into one array. This function combines the same number of elements or the same size array and returns the associative array. In array_combine() function, the array which we will declare first will be represent as a key and the array which we will declare second will be represent as a value (key value pair).
CODE =>
<?php
$arr1=["Lion","Tiger","Hyena"];
$arr2=["Dog","Cat","Mouse"];
$arrcombine=array_combine($arr1, $arr2);
print_r($arrcombine);
?>
OUTPUT =>
Array ( [Lion] => Dog [Tiger] => Cat [Hyena] => Mouse )
Q14. What is meant by array_merge()? Explain with an example.
Ans: array_merge() can merge the arrays one after another. It returns the index array or numerical array.
CODE =>
<?php
$arr1=["Ayan","Parnashri","Sayanti"];
$arr2=["Anish","Debanjan"];
$arr3=["Shounak","Arijit","Ashish"];
$arr4=["Preti"];
$arrmerge=array_merge($arr1,$arr2,$arr3,$arr4);
print_r($arrmerge);
?>
OUTPUT =>
Array ( [0] => Ayan [1] => Parnashri [2] => Sayanti [3] => Anish [4] => Debanjan [5] => Shounak [6] => Arijit [7] => Ashish [8] => Preti )
Q15. What is meant by array_count_values()? Explain with an example.
Ans: array_count_values() is to count the time of the values we have used in an array.
CODE =>
<?php
$arr=[1,2,2,3,3,3,4,4,4,4,5,5,5,5,5];
$arrcntvls=array_count_values($arr);
print_r($arrcntvls);
?>
OUTPUT =>
Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
Q16. What is the function of count()?
Ans: The function of count() is to count the total values in an array.
Q17. What is meant by sizeof()? Explain with an example.
Ans: sizeof() defines the size of an array that is how many cell is present in an array.
CODE =>
<?php
$arr1=[2];
$arr2=[3,6];
$arr3=[5,10,15];
$arrmerge=array_merge($arr1,$arr2,$arr3);
$sizeof=sizeof($arrmerge);
print_r($sizeof);
?>
OUTPUT =>
6
Q18. What is meant by explode()? Explain with an example.
Ans: explode() function converts from string into array.
CODE =>
<?php
$str="I love playing cricket";
$exp=explode(" ", $str);
print_r($exp);
?>
OUTPUT =>
Array ( [0] => I [1] => love [2] => playing [3] => cricket )
Q19. What is meant by implode()? Explain with an example.
Ans: implode() function converts from array into string.
CODE =>
<?php
$arr=["Playing","cricket","is","my","hobby"];
$imp=implode(" ", $arr);
echo "$imp";
?>
OUTPUT =>
Playing cricket is my hobby
Q20. What is meant by is_array() function?
Ans: is_array() function can check whether the variable is in form of array or not.
Q21. Provide an example of code to check whether your variable is in form of array or not?
Ans: CODE =>
<?php
$arr=[50,100,150,200];
if (is_array($arr)) {
echo "It is an array";
}
else{
echo "It is not an array";
}
?>
OUTPUT =>
It is an array
Comments
Post a Comment