Saturday 10 May 2014

To Find Second Largest Value From An Array

Here is the example to find the second largest value from an array.

array.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Array</title>
</head>

<body>
<h1>To Find Second Largest Value From An Array</h1>
<?php 
function second_largest($arr)
{
sort($arr,SORT_NUMERIC);
return($arr[count($arr)-2]);
}
echo "Second Largest Value From Array is :".second_largest(array(5,4,3,1,8,9,10,2));
?>
</body>
</html>

OUTPUT :
To Find Second Largest Value From An Array
Second Largest Value From Array is :9

No comments:

Post a Comment

Printing first 50 Fibonacci numbers Using PHP Program

Fibonacci series is a series of numbers in which each number is the sum of the two preceding numbers. For example. 0 , 1 , 1 , 2 , 3 , 5...