05 May 2011

interview questions: PHP arrays

Topic: PHP Arrays

Faculty Name:Saritha G Pillai

(Q) How do I find out if a value is already in an array?

(Ans) PHP provides a very useful function called in_array that lets you check whether a value is already in the array or not. Here is an example of it in use when we want to check if certain numbers are present in an array or not:

<?php
$numbers = range(1,10);
$checknumbers = array(1,20,10);
foreach($checknumbers as $isitthere) { 
if(in_array($isitthere,$numbers)) { echo "The number $isitthere is in the numbers array\n\n"; }
else { echo "The number $isitthere is NOT in the numbers array\n\n"; }
}
?>

This prints:

The number 1 is in the numbers array

The number 20 is NOT in the numbers array
The number 10 is in the numbers array 


(Q)How can I sort a multi-dimensional array?

(Ans
)This can easily be done using the function in PHP that is called array_multisort.

Here is an example of this function in action:

<?php

$multiarray = array(array(5,4,3,2,1),array(7,8,2,9,10));

array_multisort($multiarray[0],SORT_ASC);

print_r($multiarray);
?> 

(Q) How can I create an array of the letters of the alphabet?
(Ans) This is done using the range operator which will create an array of the letters:

<?php
$letters = range(a,z);
?>

This will give you an array from A to Z.


(Q)How do you remove duplicate values from an array?
(Ans)  Duplicate values are easily removed using the array_unique function:

<?php
$values = array("banana","apple","pear","banana");
$values = array_unique($values);
print_r($values);
?>

Outputs:

Array
(
[0] => banana
[1] => apple
[2] => pear
)


(Q) What is the difference between explode and split?
(Ans) 

- Split function splits string into array by regular expression.
Ex. split(" :", " Jaipur : Delhi : Noida ");
- Explode splits a string into array by string.
Ex. explode(" and", "Jaipur and Delhi and Noida"); 

(Q) What is the function of array_intersect()?
(Ans)   
array_intersect() can find common elements in given arrays without modifying originals. Ex- array_intersect($arr1, $arr2); 

(Q) What is the function of array_slice()?
(Ans)
 array_slice()- It can copy some elements from an array to another.
Ex 1- array_slice($arr1, 4,5);
4 is index no to start from left.
5 is no. of elements to extract.
Ex 2- array_slice($arr1, -4,5);
-4 is index no to start from right.
5 is no. of elements to extract.

(Q) What is the function of floor()?
(Ans)
 array_floor()- It can convert a float no. into lower integer no.
Ex- array_floor(4.5);
Output is 4 

(Q) What is the function of ceil()?
(Ans)
 ceil()- It can convert a float no. into upper integer no.
Ex- ceil(4.7);
Output is 5 

Faculty Name:Arun Pallisseri
Q-1: What is an array and give it's types?
Ans:
An array is a collection of similar types of elements that have the same properties.

Q-2: How can we count the number of elements of an array?
Ans:
There are two ways:
1. sizeof($arrayname) This function is an alias of count()
2. count($arrayname) If u just pass a simple variable instead of an array it will return 1.
http://php.net/manual/en/function.count.php

Q-3: What are the different functions in sorting an array?
Ans:
Sorting functions in PHP:
sort- Ascending order
rsort- Descending order
asort- Elements of associative array are sorted in ascending order.
arsort- Elements of associative array are sorted in descending order.
ksort- Keys of associative array are sorted in ascending order.
krsort- Keys of associative array are sorted in descending order.
uksort- Sorts an array by element keys using user-defined sorting (comparision) function.
natsort- Sorts an array elements using a “natural-order” algorithm. Ex- If 10, 2 are sorted by computer, then it treats 10 less than 2 because 1st number 1 is less than 2 while “natural-order” algo treats it 2 less than 10.
Q-3: What is the difference between explode and split?
Ans:

- Split function splits string into array by regular expression.
Ex. split(" :", " Jaipur : Delhi : Noida ");
- Explode splits a string into array by string.
Ex. explode(" and", "Jaipur and Delhi and Noida");


Q-4: What are the differences between mysql_fetch_array(), mysql_fetch_object(), mysql_fetch_row()?
Ans:

mysql_fetch_array() - Fetch a result row as a combination of associative array and numeric array.
mysql_fetch_row() - Fetch a result set as a numeric array().
mysql_fetch_object() - Fetch a result row as an object with properties that correspond to the fetched row and moves the internal data pointer ahead or FALSE if there are no more rows.

Q-5: How can we remove duplicate values from an array?
Ans:
Using array_unique() method.
http://php-programming-tutorial.com/php-array-functions/

Q-6: What is the task of array_key_exists()?
Ans:

array_key_exists- Checks given key in an array.
Q-7: How to print an array?
Ans:
print_r() is used to print elements of an array.


Q-8: What is the function of array_merge()?
Ans:
array_merge() can merge or combine a no. of arrays without modifying originals. Ex- array_merge($arr1, $arr2, $arr3, …);
http://www.plus2net.com/php_tutorial/function-array.php

Q-9: What is the function of array_unique()?
Ans:
array_unique() can find unique elements of a no. of arrays without modifying originals. First we have to merge them.
Ex- array_unique(array_merge($arr1, $arr2, $arr3, …));
http://nodehead.com/php/array-functions/

Q-10: What is the function of array_intersect()?
Ans:
array_intersect() can find common elements in given arrays without modifying originals. Ex- array_intersect($arr1, $arr2);
http://www.phptutorial.info/?call-user-func-array

Q-11: What is the function of array_sum()?
Ans:
array_sum() can find addition of elements of an array without modifying originals. Ex- array_sum($arr1);
http://www.expertsguide.info/2010/07/usefull-php-array-functions/

Q-12: What is the function of array_diff()?
Ans:
array_diff() can find different elements in in 1st array which are not present in 2nd array. Ex- array_diff($arr1, $arr2);
Q-13: What is the function of array_slice()?
Ans:
array_slice()- It can copy some elements from an array to another.
Ex 1- array_slice($arr1, 4,5);
4 is index no to start from left.
5 is no. of elements to extract.
Ex 2- array_slice($arr1, -4,5);
-4 is index no to start from right.
5 is no. of elements to extract.

Q-14: What is the function of floor()?
Ans:
array_floor()- It can convert a float no. into lower integer no.
Ex- array_floor(4.5);
Output is 4

Q-14: What is the function of ceil()?
Ans:
ceil()- It can convert a float no. into upper integer no.
Ex- ceil(4.7);
Output is 5

Q-14: What are arry_filter() & array_map()?
Ans:
Both are used to access specific values from an array.
array_filter("array input", "function as string" )
array_map("function", "array input")


For the latest IT jobs visit  www.ipsrjobs.com 

Follow us on facebook

3 comments: