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]);
?>
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:
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
// 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