Jump to content

Arrays


chris57828

Recommended Posts

Can someone help?  I am new to PHP and trying to get some training in before I commence study in September.  Below you will see the code for a very simple Lucky Lotto number generator, which generates six numbers at random from the $numbers array via the shuffle().  Being that I am a learner, in the most simple way possible I want to place the resulting 6 numbers in a new array, so I can use other functions to ascertain comparisons and results etc. 

 

<?php




class Game
{
public $count;
public $draw = array(1,15,16,27,30,18);
public $numbers = array  (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35
,36,37,38,39,10,41,42,43,44,45,46,47,48,49);

function display($sub1)
{
$this->count = $sub1;
while ($this->count>0)
{
shuffle($this->numbers);

for($j=0;$j<6;$j++)

echo $this->numbers[$j] . " ";


echo "<br />";

shuffle($this->numbers);
$this->count--;

}
}
}
$object1 = new Game();
$object1->display(1);

?>

Link to comment
https://forums.phpfreaks.com/topic/237554-arrays/
Share on other sites

You don't need that huge array, you can simply use rand.  I revamped your code a bit:

ini_set ("display_errors", "1");
error_reporting(E_ALL);

class Game
{
   
   function __construct() {
       //Constructor
   }

   function display($max)
   {
      $results = null;
      $i = 0;
      while($i      {
         $results[] = rand(1,$max);
         echo "
";
         $i++;
      }
      print_r($results);
      return $results;
   }
}

$max = 35;
$object1 = new Game();
$object1->display($max);

?>

Link to comment
https://forums.phpfreaks.com/topic/237554-arrays/#findComment-1220709
Share on other sites

The only real problem was you forgot the curly braces on your for loop.

 

Here's another approach at your class though

 

<?php 

class Game
{
private $numbers = array(); // makes sure these numbers can't be defined outside of the class

// a special function that will be called when the class is made
// $min=1 is the default value, in case the class is made without parameters
public function __construct($min=1,$max=49)
{
	if( $max<=$min )
		throw new Exception('Max number is less than or equal to min number');
	for($i=$min;$i<=$max;$i++)
		$this->numbers[] = $i;
}

function display($sub1,$sub2)
{
	$return = '';
	while ($sub1>0)
	{
		shuffle($this->numbers);
		// changed this a bit
		$first6 = array_slice($this->numbers,0,$sub2);
		$return .= implode(', ', $first6) . "<br />\n";
		$sub1--; // Decrease this or the loop will run forever
	}
	return $return;
}

}
try {
$object1 = new Game(20,30);
echo $object1->display(2,6);

$object2 = new Game(30,20);
echo $object2->display(1,5);
} catch (Exception $e) {
echo 'Caught exception: ',  $e->getMessage();
}

?>

Link to comment
https://forums.phpfreaks.com/topic/237554-arrays/#findComment-1220712
Share on other sites

public function __construct($min=1,$max=49)
{
    if( $max<=$min )
        throw new Exception('Max number is less than or equal to min number');

    for($i=$min;$i<=$max;$i++)
        $this->numbers[] = $i;
}

 

Why not just use range() instead of creating a loop?

public function __construct($min=1,$max=49)
{
    if( $max<=$min )
        throw new Exception('Max number is less than or equal to min number');

        $this->numbers = range($min, $max);
}

Link to comment
https://forums.phpfreaks.com/topic/237554-arrays/#findComment-1220758
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.