1. array_key_exists

<?php
$search_array = array(‘azam’ => 1, ‘andriod’ => 4);
if (array_key_exists(‘azam’, $search_array)) {
echo “The ‘azam’ element is in the array”;

}
else{
echo”not found”;
}
?>

Description

The array_key_exists() function check an array for a specified key. If the required key exists, then it return true and if not exists then return false.

2. array_map

<?php
function myfunction($v)
{
return($v+$v);
}

$a=array(6,7,8,9,10);
print_r(array_map(“myfunction”,$a));
?>

Description

The array_map() function sends each value of an array to user made function and return a new value to the user made function.

3. array_diff

<?php
$a1=array(“a”=>”red”,”b”=>”yellow”,”c”=>”orange”,”d”=>”pink”);
$a2=array(“e”=>”red”,”f”=>”yellow”);
$result=array_diff($a1,$a2);
print_r($result);

?>

Description

The array_diff() function was used to compare the array and returns different values. This function compares the value of more the one array and returns those value which is contain in array1.

4. array_filter

<?php
function odd($var) {
return($var & 1);
}

function even($var) {
return(!($var & 1));
}

$array1 = array(“a”=>1, “b”=>2, “c”=>3, “d”=>4, “e”=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);

echo “Odd :\n”;
print_r(array_filter($array1, “odd”));

echo “Even:\n”;
print_r(array_filter($array2, “even”));
?>

Description

The array_filter() function filter the values with user made function. Each value
passes from the call back function and return true value which we required.

5. array_combine

<?php
$a=array(“a”=>”azam”,”b”=>”andriod”,”c”=>”bubbles”);
echo array_search(“andriod”,$a);
?>
<h2>array_combine</h2>
<?php
$fname=array(“Muhammad”,”andriod”,”atif”);
$lname=array(“azam”,”bubbles”,”noman”);

$c=array_combine($fname,$lname);
print_r($c);
?>

Description

The array_combine() function creates an array by using from one key array and one value array. Key array was used as an index. The element in both array should be same.

6. array_unique

<?php
$a=array(“a”=>”Muhammad”,”b”=>”Azam”,”c”=>”Azam”);
print_r(array_unique($a));
?>

Description

The array_unique() function give the unique values and remove the duplicate value. If two or more array contains the same value, then it should be removed and consider the value of first array which become first in an array.

7.in_array

<?php
$people = array(“Muhammad”, “Azam”, “Andriod”, “Bubbles”);

if (in_array(“Andriod”, $people))
{
echo “Match found”;
}
else
{
echo “Match not found”;
}
?>

Description

The in_array() function was use to searches a specifies value which we define in array. If the value finds, then it returns true otherwise value not found.

Advertisements