-1

I am trying to find lowest of odd numbers in an array.

Here is my code:

$a=array(81,10,6,71,13,61,8,16,0,9,12);
$b=count($a);
for($i=0;$i<$b;$i++)
{
    if($a[$i]/2!=0)
    {
        $flag=0;

        for($j=0;$j<$b;$j++)
        {
            if($a[$j]<=$a[$i] and $a[$j]/2!=0)
            {
                $a[$i]=$a[$j];
                $flag=1;        
            }
        }

        if($flag==1)
        {
            echo('lowest odd number is'.$a[$i]);
        }

        break;
    }

    break;
}

I am not looking for new logic I am just trying to find an error in the above code. Why is it not working?

4
  • "not working" is never a sufficient explanation of a problem. what are you expecting it to do and what is it doing instead?
    – jbabey
    Jan 7, 2013 at 20:09
  • try sorting your array and with a loop return the first nummer odd number.
    – algorhythm
    Jan 7, 2013 at 20:12
  • 1
    try this code phpfiddle.org/main/code/qu0-e9s
    – algorhythm
    Jan 7, 2013 at 20:29
  • thanks for your good reply...algorhythm,,, its working.. god bless you
    – Friend
    Jan 7, 2013 at 20:32

1 Answer 1

5

You want mod (%) instead of division (/), as in

if($a[$i]%2!=0)
1
  • the same here $a[$j]/2!=0
    – algorhythm
    Jan 7, 2013 at 20:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.