Tag Archives: Functions

array_map

It applies the callback to the elements of the given arrays

Example

<?php
function squr($arg){
return($arg * $arg );
}
echo “<pre>”;
$array1 = array(1,7,5,1,9,8,4);
$array2 = array_map(“squr”, $array1);
print_r($array2);
?>

Output

Array
(
[0] => 1
[1] => 49
[2] => 25
[3] => 1
[4] => 81
[5] => 64
[6] => 16
)

array_keys

It returns all the keys or a subset of the keys of an array

Example

<?php
echo “<pre>”;
$array = array(PHP => 11, “ASP” => “22”);
print_r(array_keys($array));
?>

Output

Array
(
[0] => PHP
[1] => ASP
)

array_key_exists

It  checks if the given key or index exists in the array

Example

<?php
 $array = array('PHP' => 1, 'JSP' => 4);
 if (array_key_exists('PHP', $array)) echo "The 'PHP' element is in the array";
 ?>

Output

The ‘PHP’ element is in the array

array_intersect

It finds the intersection of arrays

Example

<?php
echo “<pre>”;
$array1 = array(“a” => “PHP”, “ASP”, “JSP”);
$array2 = array(“b” => “PHP”, “ASP”, “JSP”);
$array3 = array_intersect($array1, $array2);
print_r($array3);
?>

Output

Array
(
[a] => PHP
[0] => ASP
[1] => JSP
)


array_intersect_ukey

It finds the intersection of arrays using a callback function on the keys for comparison

Example

<?php
function compare_keys($key1, $key2)
{
if ($key1 == $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}

echo “<pre>”;
$array1 = array(‘key1’  => PHP, ‘key2’  => 11, ‘key3’  => 22, ‘key4’ => 22);
$array2 = array(‘key5’ => 5, ‘key1’ => 55, ‘key7’ => 44, ‘key8’   => 55);
print_r(array_intersect_ukey($array1, $array2, ‘compare_keys’));
?>

Output

Array
(
[key1] => PHP
)

array_intersect_uassoc

It finds the intersection of arrays with additional index check, compares indexes by a callback function

Example

<?php
echo “<pre>”;
$array1 = array(“a” => “asp”, “b” => “php”, “c” => “JSP”, “VB”);
$array2 = array(“a” => “ASP”, “B” => “php”, “JSP”, “VB”);
print_r(array_intersect_uassoc($array1, $array2, “strcasecmp”));
?>

Output

Array
(
[b] => php
)

array_intersect_key

It finds the intersection of arrays using keys for comparison

Example

<?php
echo “<pre>”;
$array1 = array(“a” => “PHP”, “b” => “ASP”, “c” => “JSP”, “VB”);
$array2 = array(“a” => “PHP”, “b” => “AS”, “HTML”);
$array3 = array_intersect_key($array1, $array2);
print_r($array3);
?>

Output

Array
(
[a] => PHP
[b] => ASP
[0] => VB
)

array_intersect_assoc

It finds the intersection of arrays with additional index check

Example

<?php
echo “<pre>”;
$array1 = array(“a” => “PHP”, “b” => “ASP”, “c” => “JSP”, “VB”);
$array2 = array(“a” => “PHP”, “b” => “AS”, “HTML”);
$array3 = array_intersect_assoc($array1, $array2);
print_r($array3);
?>

Output

Array
(
[a] => PHP
)

array_flip

It exchanges all keys with their associated values in an array

Example

<?php
echo “<pre>”;
$array1 = array(“ASP” => WEB, “PHP” => WEB, “JSP” => SEREVR);
$array2 = array_flip($array1);
print_r($array2);
?>

Output

AArray
(
[WEB] => PHP
[SEREVR] => JSP
)

array_filter

It filters elements of an array using a callback function

Example

<?php
function checkOdd($arg){
return($arg & 1);
}
echo “<pre>”;
$array1 = array(1,2, 3, 4, 5, 17, 12);
print_r(array_filter($array1, “checkOdd”));
?>

Output

Array
(
[0] => 1
[2] => 3
[4] => 5
[5] => 17
)