Array & Its Functions in PHP – Part 3

In previous two blogs we define array in php, types of array and its functions. In this blog we will discuss about more array functions. For new users it is suggested to go through the previous blogs first. Links are given below

Array & Its Functions in PHP – Part 1
Array & Its Functions in PHP – Part 2

array in php part 3

 

array_uintersect() Distinct arrays, and returns the matches (using a user-defined key comparison function, it compare only values)
   

Example :

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

Result : Array ( [c] => php )

 

array_uintersect_assoc() Distinct arrays, and returns the matches (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"=>"php","b"=>"data structure","c"=>"android");
$a2=array("a"=>"php","b"=>"android","c"=>"data structure");
$result=array_uintersect_assoc($a1,$a2,"myfunction");
print_r($result);
?>
</body>
</html>

Result : Array ( [a] => php )

 

array_uintersect_uassoc() Distinct arrays, and returns the matches (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"=>"html","b"=>"java","c"=>"php");
$a2=array("a"=>"html","b"=>"java","c"=>"java");
$result=array_uintersect_uassoc($a1,$a2,"myfunction_key","myfunction_value");
print_r($result);
?>
</body>
</html>

Result : Array ( [a] => red [b] => green )

 

array_unique() Eliminates identical values from an array
   

Example :

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

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

 

array_unshift() Insert one or more elements to the beginning of an array
   

Example :

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

Result : Array ( [0] => java [a] => data structure [b] => c++ )

 

array_values() Returns an array with all the values
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$a=array("Name"=>"CPD","Age"=>"7","Country"=>"India");
print_r(array_values($a));
?>
</body>
</html>

Result : Array ( [0] => CPD [1] => 7 [2] => India )

 

array_walk() It applies a user function to every member of an array
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
function myfunction($value,$key)
{
echo "The key $key has the value $value<br>";
}
$a=array("a"=>"C++","b"=>"data structure","c"=>"android");
array_walk($a,"myfunction");
?>
</body>
</html>

Result :The key a has the value C++
The key b has the value data structure
The key c has the value android

 

array_walk_recursive() It applies a user function recursively to every member of an array
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
function myfunction($value,$key)
{
echo "The key $key has the value $value<br>";
}
$a1=array("a"=>"android","b"=>"data structure");
$a2=array($a1,"1"=>"C++","2"=>"java");
array_walk_recursive($a2,"myfunction");
?>
</body>
</html>

Result : The key a has the value android
The key b has the value data structure
The key 1 has the value C++
The key 2 has the value java

 

arsort() Sorts an associative array in declining order, according to the value
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$age=array("html"=>"5","java"=>"8","php"=>"6");
arsort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
</body>
</html>

Result : Key=html, Value=5
Key=java, Value=8
Key=php, Value=6

 

asort() Sorts an associative array in climbing order, according to the value
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$age=array("php"=>"6","java"=>"8","html"=>"5");
asort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
</body>
</html>

Result : Key=php, Value=6
Key=java, Value=8
Key=html, Value=5

 

compact() It create array containing variables and their values
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$firstname = "Cpd";
$lastname = "India";
$age = "7";
$result = compact("firstname", "lastname", "age");
print_r($result);
?>
</body>
</html>

Result : Array ( [firstname] => Cpd [lastname] => India [age] => 7 )

 

count() It returns the number of elements in an array
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$programming_language=array("C","C++","C#");
echo count($programming_language);
?>
</body>
</html>

Result : 3

 

current() It returns the current element in an array
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$people = array("C#", "java", "android", "php");
echo current($people) . "<br>";
?>
</body>
</html>

Result : C#

 

each() It returns the current value and key pair from an array
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$people = array("php", "C#", "data structure", "java");
print_r (each($people));
?>
</body>
</html>

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

 

end() It sets the internal pointer of an array to its last element
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$people = array("seo", "data structure", "C#", "java");
echo current($people) . "<br>";
echo end($people);
?>
</body>
</html>

Result : seo
java

 

extract() It imports variables into the current symbol table from an array
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$a = "Original";
$my_array = array("a" => "C#","b" => "Data structure", "c" => "java");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c";
?>
</body>
</html>

Result : $a = C#; $b = Data structure; $c = java

 

in_array() It checks if a specified value exists in an array
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$people = array("java", "data structure", "C#", "php");
if (in_array("C#", $people))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?>
</body>
</html>

Result : Match found

 

key() It fetches a key from an array
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$people=array("cloud computing","java","C#","php");
echo "The key from the current position is: " . key($people);
?>
</body>
</html>

Result : The key from the current position is: 0

 

krsort() It sorts an associative array in declining order, according to the key
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$age=array("cloud computing"=>"68","android"=>"17","seo"=>"43");
krsort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
</body>
</html>

Result : Key=seo, Value=43
Key=cloud computing, Value=68
Key=android, Value=17

 

ksort() It sorts an associative array in ascending order, according to the key
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$age=array("seo"=>"35","android"=>"37","cloud computing"=>"43");
ksort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
</body>
</html>

Result : Key=android, Value=37
Key=cloud computing, Value=43
Key=seo, Value=35

 

list() It assigns variables as if they were an array
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$my_array = array("Horse","Cat","Dog");
list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
?>
</body>
</html>

Result : I have several animals, a Horse, a Cat and a Dog.

 

natcasesort() It sorts an array using a case insensitive "natural order" algorithm
   

Example :

<?php
$temp_files = array("temp15.txt","Temp55.txt",
"temp3.txt","Temp32.txt","temp21.txt");
natsort($temp_files);
echo "Natural order: ";
print_r($temp_files);
echo "<br />";
natcasesort($temp_files);
echo "Natural order case insensitve: ";
print_r($temp_files);
?>

Result : Natural order:
Array
(
[0] => Temp12.txt
[1] => Temp32.txt
[2] => temp3.txt
[4] => temp21.txt
[3] => temp55.txt
)
Natural order case insensitve:
Array
(
[2] => temp1.txt
[4] => temp2.txt
[0] => Temp10.txt
[3] => temp15.txt
[1] => Temp22.txt
)

 

natsort() It sorts an array using a "natural order" algorithm
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$temp_files = array("temp15.txt","temp10.txt",
"temp1.txt","temp22.txt","temp2.txt");
sort($temp_files);
echo "Standard sorting: ";
print_r($temp_files);
echo "<br>";
natsort($temp_files);
echo "Natural order: ";
print_r($temp_files);
?>
</body>
</html>

Result : Standard sorting: Array ( [0] => temp1.txt [1] => temp10.txt [2] => temp15.txt [3] => temp2.txt [4] => temp22.txt )
Natural order: Array ( [0] => temp1.txt [3] => temp2.txt [1] => temp10.txt [2] => temp15.txt [4] => temp22.txt )

 

next() It advances the internal array pointer of an array
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>";
echo next($people);
?>
</body>
</html>

Result : Peter
Joe

 

pos() Alias of current()
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo pos($people) . "<br>";
?>
</body>
</html>

Result : Peter

 

 

prev() It rewinds the internal array pointer
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>";
echo next($people) . "<br>";
echo prev($people);
?>
</body>
</html>

Result : Peter
Joe
Peter

 

range() It creates an array containing a range of elements
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$number = range(0,5);
print_r ($number);
?>
</body>
</html>

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

 

reset() It sets the internal pointer of an array to its first element
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>";
echo next($people) . "<br>";
echo reset($people);
?>
</body>
</html>

Result :Peter
Joe
Peter

 

rsort() It sorts an indexed array in declining order
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$cars=array("Volvo","BMW","Toyota");
rsort($cars);
$clength=count($cars);
for($x=0;$x<$clength;$x++)
{
echo $cars[$x];
echo "<br>";
}
?>
</body>
</html>

Result : Volvo
Toyota
BMW

 

shuffle() It shuffles an array
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$my_array = array("php","c#","cloud computing","data structure","java");
shuffle($my_array);
print_r($my_array);
?>
<p>Refresh the page to see how shuffle() randomizes the order of the elements in the array.</p>
</body>
</html>

Result : Array ( [0] => cloud computing [1] => java [2] => c# [3] => php [4] => data structure)
Refresh the page to see how shuffle() randomizes the order of the elements in the array.

 

sizeof() Alias of count()
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$cars=array("cloud computing","java","android");
echo sizeof($cars);
?>
</body>
</html>

Result : 3

 

sort() It sorts an indexed array in ascending order
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
$cars=array("java","android","cloud computing");
sort($cars);
$clength=count($cars);
for($x=0;$x<$clength;$x++)
{
echo $cars[$x];
echo "<br>";
}
?>
</body>
</html>

Result : android
cloud computing
java

 

uasort() It sorts an array by values using a user-defined comparison function
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$arr=array("a"=>25,"b"=>13,"c"=>37,d=>"80");
uasort($arr,"my_sort");
foreach($arr as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
</body>
</html>

Result : Key=b, Value=13
Key=a, Value=25
Key=c, Value=37
Key=d, Value=80

 

 

uksort() It sorts an array by keys using a user-defined comparison function
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$arr=array("a"=>14,"b"=>20,"c"=>8,d=>"4");
uksort($arr,"my_sort");

foreach($arr as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
</body>
</html>

Result : Key=a, Value=4
Key=b, Value=2
Key=c, Value=8
Key=d, Value=6

 

 

usort() It sorts an array using a user-defined comparison function
   

Example :

<!DOCTYPE html>
<html>
<body>
<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$a=array(15,80,38,2);
usort($a,"my_sort");
$arrlength=count($a);
for($x=0;$x<$arrlength;$x++)
{
echo $a[$x];
echo "<br>";
}
?>
</body>
</html>

Result : 2
15
38
80

 

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

Please follow and like us:
20

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

Submit Query