Monday, May 14

Very Simple way to Find Second largest number in given array using PHP

Hi,


Finding the Second largest number in  a array using PHP.

Solution: 

<?php
// PHP program to sort in ascending
// order using uasort() function

// user_defined comparator function
function sorting($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}

// input array
$arr=array(245,268,289,10,999,585,120);
//uasort($arr,"sorting");
usort($arr,"sorting");

 $kesy =(sizeof($arr));
// printing sorted array.
print_r($arr[$kesy-2]);

?>




Output: 585


___________________________________________________________________


Finding the Second largest number in  a array having Key using PHP.

Solution: 

<?php
// PHP program to sort in ascending
// order using uasort() function

// user_defined comparator function
function sorting($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}

// input array
$arr=array("a"=>2044,"b"=>422,"g"=>748,"d"=>46,"e"=>421,"f"=>59);

//uasort($arr,"sorting");
usort($arr,"sorting");

 $kesy =(sizeof($arr));
// printing sorted array.
print_r($arr[$kesy-2]);


?>


OUTPUT: 748

No comments:

Post a Comment

Backup files to google drive using PHP coding

 Dear All, To backup files to Google Drive using PHP coding, you can use the Google Drive API and the Google Client Library for PHP. Here...