Category Archives: PHP

array_uintersect

It finds  the intersection of arrays, compares data by a callback function

Example

<?php
echo “<pre>”;
$array1 = array(“a” => “php”, “b” => “asp”, “c” => “AS”, “JS”);
$array2 = array(“a” => “PHP”, “B” => “asp”, “AS”, “JS”);
print_r(array_uintersect($array1, $array2, “strcasecmp”));
?>

Output

Array
(
[a] => php
[b] => asp
[c] => AS
[0] => JS
)

array_uintersect_uassoc

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

Example

<?php
echo “<pre>”;
$array1 = array(“a” => “php”, “b” => “asp”, “c” => “AS”, “JS”);
$array2 = array(“a” => “PHP”, “B” => “asp”, “AS”, “JS”);
print_r(array_uintersect_uassoc($array1, $array2, “strcasecmp”, “strcasecmp”));
?>

Output

Array
(
[a] => php
[b] => asp
)

array_uintersect_assoc

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

Example

<?php
function func1($arg1,$arg2)
{
if ($arg1===$arg2)
{
return 0;
}
return 1;
}
echo “<pre>”;
$array1=array(“a”=>”PHP”,”b”=>”JSP”,”c”=>”AS”);
$array2=array(a=>”PHP”,b=>”ASP”,c=>”JS”);
print_r(array_uintersect_assoc($array1,$array2,”func1″));
?>

Output

Array
(
[a] => PHP
)

array_udiff

It finds the difference of arrays by using a callback function for data comparison

Example

<?php
function func1($v1,$v2)
{
if ($v1===$v2)
{
return 0;
}
return 1;
}
echo “<pre>”;
$array1=array(“a”=>”PHP”,”b”=>”JSP”,”c”=>”AS”);
$array2=array(1=>”PHP”,2=>”ASP”,3=>”JS”);
print_r(array_udiff($array1,$array2,”func1″));
?>

Output

Array
(
[b] => JSP
[c] => AS
)

array_udiff_uassoc

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

Example

<?php
function func1($arg1,$arg2)
{
if ($arg1===$arg2)
{
return 0;
}
return 1;
}

function func2($arg1,$arg2)
{
if ($arg1===$arg2)
{
return 0;
}
return 1;
}
echo “<pre>”;
$array1=array(“a”=>”PHP”,”b”=>”JSP”,”c”=>”ASP”);
$array2=array(“a”=>”PHP”,”b”=>”AS”,”c”=>”Js”);
print_r(array_udiff_uassoc($array1,$array2,”func1″,”func2″));
?>

Output

Array
(
[b] => JSP
[c] => ASP
)

array_udiff_assoc

It finds the difference of arrays with additional index check, compares data by a callback function

Example

<?php
function func($arg1,$arg2)
{
if ($arg1===$arg2)
{
return 0;
}
return 1;
}
echo “<pre>”;
$array1=array(“a”=>”PHP”,”b”=>”JSP”,”c”=>”ASP”);
$array2=array(“a”=>”PHP”,”b”=>”AS”,”c”=>”JS”);
print_r(array_udiff_assoc($array1,$array2,”func”));
?>

Output

Array
(
[b] => JSP
[c] => ASP
)

array_splice

It removes a portion of the array and replace it with something else

Example

<?php
echo “<pre>”;
$array = array(0 => ‘PHP’, 1 => ‘ASP’, 2 => ‘JSP’, 3 => ‘JS’);
$array1=array_splice($array,3);
print_r($array1);
?>

Output

Array
(
[0] => JS
)

array_slice

It extracts a slice of the array

Example

<?php
echo “<pre>”;
$array = array(0 => ‘PHP’, 1 => ‘ASP’, 2 => ‘JSP’, 3 => ‘JS’);
$array1=array_slice($array,2);
print_r($array1);
?>

Output

Array
(
[0] => JSP
[1] => JS
)

array_shift

It shift an element off the beginning of array

Example

<?php
echo “<pre>”;
$array = array(0 => ‘PHP’, 1 => ‘ASP’, 2 => ‘JSP’, 3 => ‘JS’);
array_shift($array);
print_r($array);
?>

Output

Array
(
[0] => ASP
[1] => JSP
[2] => JS
)