Home > PHP > Basic Questions on PHP Part2

Basic Questions on PHP Part2

November 16, 2009 Leave a comment Go to comments
After getting a very good response on my earlier compilation “Basic Questions on PHP “, I am back with a new compilation of basic PHP questions on arrays.
1- How to check if a value exists in an array?
Ans Use in_array() function for this.
Example:
<?php
$sample = array(“banana”,”apple”,”pear”,”banana”);
$newvalue = “pear”;
if (in_array($newvalue,$sample)) { echo “$newvalue is already in the array!”; }
?>
2- How to create an array of consecutive numbers and array of alphabets?
Ans:
<?php
$arr_of_numbers = range(1,10);//$arr_of_numbers = array(1,2,3,4,5,6,7,8,9,10)
$arr_of_letters = range(a,m); //$arr_of_letters = array(a,b,c,d,e,f,g,h,i,j,k,l,m)
?>
3- How to reset an array?
Ans: The reset() function does this and returns the first element of the array.
$sample = array(‘itm1′,’itm2′,’itm3′,’itm4’);
$first = reset($sample);
echo $first; //itm1
4- How to count the frequency a value appears in an array?
Ans: array_count_values() function does this operation. It returns an associative array with values as keys and their fequencies as values.
<?php
$sample_arr = array(1,0,1,2,0,2,2,2,3,4,2,5,2,3,5,3,1,5);
$most_popular = array_count_values($sample_arr);
print_r($most_popular);
?>
output: Array(
[1] => 3
[0] => 2
[2] => 6
[3] => 3
[4] => 1
[5] => 3
)
5- How to remove last element of an array?
Ans: array_pop() function does this.
$sample = array(“One”,”Two”,”Three”,”Four”,”Five”,”Six”);
$last = array_pop($sample);
echo “last element is “.$last; //last element is Six…
6- How to sort an array with alphanumeric values?
Ans: natsort() function will do this.
Example:
<?php
$sample = array(“1.gif”,”2.png”,”20.jpg”,”10.gif”);
natsort($sample);
print_r($sample);
?>
Output:
Array (
[0] => 1.gif
[1] => 2.png
[3] => 10.gif
[2] => 20.jpg
)
7- How to change an array into string?
Ans: We can achieve this using “implode()” function.
Example:
<?php
$sample = array(“I”,”love”,”PHP”);
$sample_to_string = implode(” “, $sample);
echo “$sample_to_string”;//I love PHP
?>
8- How to remove duplicate values from an array?
Ans: The array_unique() function will do this.
Example:
<?php
$sample = array(“pear”,”apple”,”pear”,”banana”);
$sample_to_unique = array_unique($sample);
print_r($sample_to_unique);
?>
Output:
Array
(
[0] => pear
[1] => apple
[2] => banana
)
9- How to interchange key to values and visa versa of an array?
Ans: We can do this using array_flip() function.
Example:
<?php
$sample = array(“John”,”Alice”,”Boby”);
print_r($sample);
$flipped = array_flip($sample);
print_r($flipped);
?>
Output:
Array
(
[0] => John
[1] => Alice
[2] => Boby
)
Array
(
[John] => 0
[Alice] => 1
[Boby] => 2
)
10- How to add an element at the begining of an array?
Ans: The array_unshift() does this. This function returns the number of elements in the array after additions of the new element.
Example:
<?php
$sample = array(2,3,4,5,6);
$no_sample = array_unshift($sample,1);
echo “Number of elements: $no_sample \n\n”;
print_r($sample);
?>
Output:
Number of elements: 10
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
Hope these questions will help the PHP folks. I will come soon with some new PHP stuff.
Advertisements
Categories: PHP Tags: , ,
  1. Karthick
    June 9, 2011 at 10:23 am

    I want to become a PHP developer and i am 2011 pass out student, searching PHP jobs, If any PHP programmer working in any company means, please help me to get a job. I will not forget you in my Life Time, Thank u. mail me at (mail2skarthick@gmail.com)

  2. Karthick
    June 9, 2011 at 10:24 am

    I want to become a PHP developer and i am 2011 pass out student, searching PHP jobs, If any PHP programmer working in any company means, please help me to get a job. I will not forget you in my Life Time, Thank u. mail me at (mail2skarthick@gmail.com)..

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: