PHP -Write a program to find biggest number in an array where array can also have nested arrays.
For example given an array like
Input : (1, 0, 6, 9, array(100, -1, 10, 7), 40, array(101, array(120, 240), 180), 200)
that can contain numbers and nested arrays containing numbers, we need to return the maximum number.
Output : 240
Program:
<?php
$inputArray = array(1, 0, 6, 9, array(100, -1, 10, 7), 40, array(101, array(120, 240), 180), 200);
echo findBiggest($inputArray);
function findBiggest($inputArray)
{
static $biggest = 0;
if(is_array($inputArray))
{
foreach($inputArray as $arr)
{
findBiggest($arr);
}
}
else
{
if($inputArray > $biggest)
$biggest = $inputArray;
}
return $biggest;
}
For example given an array like
Input : (1, 0, 6, 9, array(100, -1, 10, 7), 40, array(101, array(120, 240), 180), 200)
that can contain numbers and nested arrays containing numbers, we need to return the maximum number.
Output : 240
Program:
<?php
$inputArray = array(1, 0, 6, 9, array(100, -1, 10, 7), 40, array(101, array(120, 240), 180), 200);
echo findBiggest($inputArray);
function findBiggest($inputArray)
{
static $biggest = 0;
if(is_array($inputArray))
{
foreach($inputArray as $arr)
{
findBiggest($arr);
}
}
else
{
if($inputArray > $biggest)
$biggest = $inputArray;
}
return $biggest;
}
help for weak students,study online here
ReplyDeletehttp://www.kidsfront.com/govt-jobs-exams.html