First we are iterate the array with help of foreach
loop. in foreach
loop we check $result
is less than array value. if array
value is greater then $result
array then we override previous value of $result
with new value. if we found greatest length than previous array element then we are storing new length & key of new element in variable.
$array =array("AB","ABC","ABCD","ABCDE","BD");
$result = $resultkey = 0;
foreach($array as $key=>$value) {
if($result < strlen($value) ) {
$result = strlen($value);
$resultkey = $key;
}
}
echo 'longest value:' $result;
echo 'result :' $array[$resultkey]
Output:
longest value: 5
result :ABCDE