Find Laregest And 2nd Largest element in an array without function in Php

 <?php

$testarray = array(4,3,8,5,2,9,7,6);

$length =  count($testarray);

for($i=0;$i<$length;$i++){

for($j=0;$j<$length-$i-1;$j++){

if ($testarray[$j]>$testarray[$j+1]) {

$temp = $testarray[$j];

$testarray[$j] = $testarray[$j+1];

$testarray[$j+1] = $temp;

}

}

}


echo "Largest Element == ".$testarray[$length-1+1-1];

echo"<br>";

echo "2nd Largest Element == ".$testarray[$length-2+1-1];

?>



OutPut:-

Largest Element == 9
2nd Largest Element == 8

Comments

Popular posts from this blog

Add Css And Js Files on Specific Page In laravel

Be 10x 10 tools from sessions

Diffrence between Put and Patch