array_merge

It merge arrays

Example

<?php
echo “<pre>”;
$array1 =array(1 => ‘PHP’);
$array2 = array(1 => ‘ASP’);
$array3 = array_merge($array2,$array1);
print_r($array3);
?>

Output

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


array_merge_recursive

It merges two or more arrays recursively

Example

<?php
echo “<pre>”;
$array1 = array(“PHP” => array(“WEB” => “1”), 7);
$array2 = array(17, “ASP” => array(“FAV” => “34”, “84”));
$array3 =($array1, $array2);
print_r($array3);
?>

Output

Array
(
[PHP] => Array
(
[WEB] => 1
)

[0] => 7
[1] => 17
[ASP] => Array
(
[FAV] => 34
[0] => 84
)


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
)