3

I am trying to get highest number from array. But not getting it. I have to get highest number from the array using for loop.

<?php
$a =array(1, 44, 5, 6, 68, 9);
$res=$a[0];
for($i=0; $i<=count($a); $i++){
 if($res>$a[$i]){
 $res=$a[$i];
 }
}
?>

I have to use for loop as i explained above. Wat is wrong with it?

potashin
44.7k11 gold badges92 silver badges113 bronze badges
asked Jan 4, 2015 at 0:45
1
  • 2
    $res = max($a); not working for you? Commented Jan 4, 2015 at 0:47

5 Answers 5

4

This should work for you:

<?php
 $a = array(1, 44, 5, 6, 68, 9);
 $res = 0;
 foreach($a as $v) {
 if($res < $v)
 $res = $v;
 }
 echo $res;
?>

Output:

68

In your example you just did 2 things wrong:

$a = array(1, 44, 5, 6, 68, 9);
$res = $a[0];
for($i = 0; $i <= count($a); $i++) {
 //^ equal is too much gives you an offset!
 if($res > $a[$i]){
 //^ Wrong condition change it to < 
 $res=$a[$i];
 }
}

EDIT:

With a for loop:

$a = array(1, 44, 5, 6, 68, 9);
$res = 0;
for($count = 0; $count < count($a); $count++) {
 if($res < $a[$count])
 $res = $a[$count];
}
answered Jan 4, 2015 at 0:49
5
  • Sorry i have to use for loop Commented Jan 4, 2015 at 1:19
  • @pawankumar updated my answer, but your code is with a for loop and i showed you where to fix it, added now and example how it should look like Commented Jan 4, 2015 at 1:23
  • I need only highest single number (68). Commented Jan 4, 2015 at 1:24
  • @pawankumar $res is equal 68 it is that number! print it with echo Commented Jan 4, 2015 at 1:25
  • 1
    outside for loop . echo $res; i got answer. Thanks again Commented Jan 4, 2015 at 1:28
3

What about:

<?php
 $res = max(array(1,44,5,6,68,9));

(docs)

answered Jan 4, 2015 at 0:48
1

you should only remove the = from $i<=count so it should be

<?php $a =array(1,44,5,6,68,9);
$res=$a[0];
for($i=0;$i<count($a);$i++){
 if($res<$a[$i]){
 $res=$a[$i];
 }
}
?>

the problem is that your loop goes after your arrays index and the condition is reversed.

answered Jan 4, 2015 at 0:55
0

The max() function will do what you need to do :

$res = max($a);

More details here.

potashin
44.7k11 gold badges92 silver badges113 bronze badges
answered Jan 4, 2015 at 0:51
2
  • sorry i am using for loop Commented Jan 4, 2015 at 0:54
  • Sorry. I missed that bit. Commented Jan 4, 2015 at 0:59
-1

Suggest using Ternary Operator

(Condition) ? (True Statement) : (False Statement);

 <?php
 $items = array(1, 44, 5, 6, 68, 9);
 $max = 0;
 foreach($items as $item) {
 $max = ($max < $item)?$item:$max;
 }
 echo $max;
 ?>
answered Jun 24, 2020 at 15:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.