简体   繁体   中英

Display even & odd numbers

I am trying loop through an array of random numbers if the the number is dividable by two then it's even and I then want to assign this to an array $even[], and if odd then assign it to the odd array. I have managed to display the results without using an array but for the sake of this I want to put them into their own array. However I can't seem to get this result I'm after all I get is this error: message Array to string conversion.

<?php

$numbers = array();

for ($i=0; $i<=1000; $i++) {
   $numbers[]=mt_rand(1,1000);
   if ($i % 2 == 0){
    $even[]=$i;
  } else {
    $odd[]=$i;
  }
}

echo $even;
echo $odd;

?>

Try this to echo the results.

foreach ($even as $evens){
echo $evens . '<br/>';
}
define $odd and $even as a array
 $even = array();
 $odd = array();
check if($i%2 == 0)
   {
      $even[] = $i;
   }
   else
   {
      $odd = $i;
   }

var_dump($even); var_dump($odd);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM