Array & Its Functions in PHP – Part 2

array in PhpAs we discussed about Array, its types and its Functions in Php in our previous blog. We Will now continue our session about arrays functions in Php If you read this blog for the first time then I suggest you to read the previous blog first. You can get the previous blog link here

 

array_intersect_key() Compare arrays, and returns the matches (compare keys only)
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"php","b"=>"java","c"=>"android");
$a2=array("a"=>"php","c"=>"android","d"=>"cloud computing");
$result=array_intersect_key($a1,$a2);
print_r($result);
?>
</body>
</html>

Result: Array ( [a] => php [c] => android )

 

array_intersect_uassoc() Distinct arrays, and returns the matches (compare values and keys, using a user-defined key comparison function)
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"cloud computing","b"=>"php","c"=>"android");
$a2=array("d"=>"cloud computing","b"=>"php","e"=>"android");
$result=array_intersect_uassoc($a1,$a2,"myfunction");
print_r($result);
?>
</body>
</html>

Result : Array ( [b] => php )

 

array_intersect_ukey() Distinct arrays, and returns the matches (compare only keys, using a user-defined key comparison function)
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"php","b"=>"html","c"=>"java");
$a2=array("a"=>"php","c"=>"java","d"=>"android");
$result=array_intersect_key($a1,$a2);
print_r($result);
?>
</body>
</html>

Result : Array ( [a] => php [c] => java )

 

array_key_exists() Examine if the specified key exists in the array
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$a=array("java"=>"version 8","php"=>"version 6");
if (array_key_exists("java",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
</body>
</html>

Result : Key exists!

 

array_keys() Returns all the keys of an array
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$a=array("dotnet"=>"version 4.6","php"=>"version 6","java"=>"version 8",);
print_r(array_keys($a));
?>
</body>
</html>

Result : Array ( [0] => php [1] => java [2] => dotnet )

 

array_map() Sends each value of an array to a user-define function, which returns new values
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
function myfunction($num)
{
return($num*$num);
}
$a=array(4,7,2,15,13);
print_r(array_map("myfunction",$a));
?>
</body>
</html>

Result: Array ( [0] => 16 [1] => 49 [2] => 4 [3] => 225 [4] => 169 )

 

array_merge() One or more arrays will merges into one array
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("php","android");
$a2=array("C++","java");
print_r(array_merge($a1,$a2));
?>
</body>
</html>

Result: Array ( [0] => php [1] => android [2] => C++ [3] => java )

 

array_merge_recursive() One or more arrays will merges into one array recursively
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"android","b"=>"java");
$a2=array("c"=>"dotnet","b"=>"php");
print_r(array_merge_recursive($a1,$a2));
?>
</body>
</html>

Result: Array ( [a] => android [b] => Array ( [0] => java [1] => php ) [c] => dotnet )

 

array_multisort() Sorts multi-dimensional or multiple arrays
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a=array("Seo","Data structure","Java","Android","Php");
array_multisort($a);
print_r($a);
?>
</body>
</html>

Result: Array ( [0] => Android [1] => Data structure [2] => Java [3] => Php [4] => Seo )

 

array_pad() Inserts a predefined number of items, with a predefined value, to an array
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a=array("java","php");
print_r(array_pad($a,5,"android"));
?>
</body>
</html>

Result: Array ( [0] => java [1] => php [2] => android [3] => android [4] => android )

 

array_pop() Eleminates the last element of an array
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a=array("dotnet","php","html");
array_pop($a);
print_r($a);
?>
</body>
</html>

Result: Array ( [0] => dotnet [1] => php )

 

array_product() Measures the product of the values in an array
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a=array(5,5);
echo(array_product($a));
?>
</body>
</html>

Result: 25

 

array_push() Fills one or more elements to the end of an array
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a=array("dotnet","php");
array_push($a,"html","java");
print_r($a);
?>
</body>
</html>

Result: Array ( [0] => dotnet [1] => php [2] => html [3] => java )

 

array_rand() Returns one or more random keys from an array
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a=array("seo","java","php","android","C++");
$random_keys=array_rand($a,3);
echo $a[$random_keys[0]]."<br>";
echo $a[$random_keys[1]]."<br>";
echo $a[$random_keys[2]];
?>
</body>
</html>

Result: seo

java

php

 

array_reduce() Using a user-defined function, returns an array as a string
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
function myfunction($v1,$v2)
{
return $v1 . "-" . $v2;
}
$a=array("php","android","java");
print_r(array_reduce($a,"myfunction"));
?>
</body>
</html>

Result: php-android-java

 

array_replace() changes the values of the first array with the values from following arrays
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("android","C++");
$a2=array("php","java");
print_r(array_replace($a1,$a2));
?>
</body>
</html>

Result: Array ( [0] => php [1] => java )

 

array_replace_recursive() Changes the values of the first array with the values from following arrays recursively
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>array("java"),"b"=>array("php","android"),);
$a2=array("a"=>array("data structure"),"b"=>array("cloud computing"));
print_r(array_replace_recursive($a1,$a2));
?>
</body>
</html>

Result: Array ( [a] => Array ( [0] => data structure ) [b] => Array ( [0] => cloud computing [1] => android ) )

 

array_reverse() Returns an array in the reverse order
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a=array("a"=>"cloud computing","b"=>"data structure","c"=>"C++");
print_r(array_reverse($a));
?>
</body>
</html>

Result: Array ( [c] => cloud computing [b] => BMW [a] => C++ )

 

array_search() finds an array for a given value and returns the key
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a=array("a"=>"java","b"=>"php","c"=>"android");
echo array_search("java",$a);
?>
</body>
</html>

Result: a

 

array_shift() Eliminates the first element from an array, and returns the value of the eliminates element
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a=array("a"=>"cloud computing","b"=>"java","c"=>"data structure");
echo array_shift($a)."<br>";
print_r ($a);
?>
</body>
</html>

Result: cloud computing

Array ( [b] => java [c] => data structure )

 

array_slice() Returns choosen parts of an array
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a=array("php","cloud computing","dotnet","java","android");
print_r(array_slice($a,2));
?>
</body>
</html>

Result: Array ( [0] => dotnet [1] => java [2] => android )

 

array_splice() Eliminates and substitutes specified elements of an array
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a1=array("a"=>"C++","b"=>"dotnet","c"=>"html","d"=>"wordpress");
$a2=array("a"=>"data structure","b"=>"android");
array_splice($a1,0,2,$a2);
print_r($a1);
?>
</body>
</html>

Result: Array ( [0] => data structure [1] => android [c] => html [d] => wordpress )

 

array_sum() Returns the total of the values in an array
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
$a=array(22,56,94);
echo array_sum($a);
?>
</body>
</html>

Result: 172

 

array_udiff() Distinct arrays, and returns the differences (compare only values, using a user-defined key comparison function)
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"seo","b"=>"data structure","c"=>"android");
$a2=array("a"=>"android","b"=>"java","e"=>"android");
$result=array_udiff($a1,$a2,"myfunction");
print_r($result);
?>
</body>
</html>

Result: Array ( [a] => seo [b] => data structure )

.

array_udiff_assoc() Distinct arrays, and returns the differences (compare values and keys, using a built-in function to compare the keys and a user-defined function to compare the values)
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"data structure","b"=>"html","c"=>"android");
$a2=array("a"=>"data structure","b"=>"android","c"=>"html");
$result=array_udiff_assoc($a1,$a2,"myfunction");
print_r($result);
?>
</body>
</html>

Result: Array ( [b] => html [c] => android )

 

array_udiff_uassoc() Distinct arrays, and returns the differences (compare values and keys, using two user-defined key comparison functions)
   

Example:

<!DOCTYPE html>
<html>
<body>
<?php
function myfunction_key($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
function myfunction_value($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"java","b"=>"android","c"=>"html");
$a2=array("a"=>"java","b"=>"android","c"=>"android");
$result=array_udiff_uassoc($a1,$a2,"myfunction_key","myfunction_value");
print_r($result);
?>
</body>
</html>

Result: Array ( [c] => html )

 

For FREE DEMO CLASS CALL – 011-65164822 / 91- 8860352748

CPD Technologies
Block C 9/8, Sector -7, Rohini, Delhi-110085, India
Landmark: Near Rohini East Metro Station, Opposite Metro Pillar No-397

Website:- www.cpd-india.com

Email :- support@cpd-india.com

Your email address will not be published. Required fields are marked *

Contact CPD Technologies






    [recaptcha]