Jump to content

Exploding Arrays?


chris57828

Recommended Posts

Hi, I am new to programming hence why I am unfamiliar with built in PHP functions and my code is rather unsophisticated, however, I am attempting to write a small ‘Lucky Lotto’ program which involves me predetermining the winning numbers manually via the array $draw.

 

Via display() a user can decide how many ‘attempts’ they want.  I want to be able to ascertain how many winning numbers each draw has (if any) so I have constructed a rather long IF function whereby the results are ‘exploded’ into an array namely $temp or $temp1 then via count() or sizeof() I should be able to determine the results.  The problem I am having is that the arrays I am ‘exploding’ to namely $temp and $temp1 only ever appear to hold one element at position zero, which is obviously contrary to what arrays are for.  By looking at the code below can someone show me in laymen’s terms how I can get $temp and $temp1 to hold and output more than one element, thanks!  :confused:

 

<?php


class Game
{
public $temp;
public $temp1;
public $result;
public $result1;
public $draw = array(15,11,27,9,5,25);
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,40,41,42,43,44,45,46,47,48,49);
public $age;
public $count;

function __construct($sub1, $sub2)
{

$this->name = $sub1;
$this->age = $sub2;
return $this->name;age;
}
function checkAge()
{
if ($this->age <18)
{
return "Sorry $this->name, you are too young to gamble";
}
else
{
return "Hi $this->name welcome to Lucky Lotto Dip, how many draws would you like?";
}
}

function display($sub3)
{
shuffle ($this->numbers);
$this->count = $sub3;
echo "<pre>";
     echo "*********************"; 
 echo "<br />";
 echo "*    Lucky Lotto    *";
 echo "<br />";
     echo "*********************";
while ($this->count >0)

{    
     echo "<br />";
     echo "* ";
for ($j =0; $j <6; $j++) // For loop
     
     if ($this->numbers[$j] <10)
 {
 echo " " . $this->numbers[$j] . " ";
 if ($this->numbers[$j] == $this->draw[0] or $this->numbers[$j] == $this->draw[1] or $this->numbers[$j] == $this->draw[2]
 or $this->numbers[$j] == $this->draw[3] or $this->numbers[$j] == $this->draw[4] or $this->numbers[$j] == $this->draw[5]  )
 {
  $temp = explode(' ', $this->numbers[$j]);
  for ($a =0; $a <$temp.length; $j++);  
      	  
 }
}
 else
 {
 echo $this->numbers[$j] . " ";
if ($this->numbers[$j] == $this->draw[0] or $this->numbers[$j] == $this->draw[1] or $this->numbers[$j] == $this->draw[2]
 or $this->numbers[$j] == $this->draw[3] or $this->numbers[$j] == $this->draw[4] or $this->numbers[$j] == $this->draw[5]  )
 {
  $temp1 = explode(' ', $this->numbers[$j]);
  for ($a =0; $a <$temp1.length; $j++);  
      
  
 }
 }
      


 echo "* ";
 echo "<br />";
 echo "<br />";
 echo $temp[$a];
 echo "<br />";
 echo $temp1[$a];
 echo "<br />";
 echo "*********************";
 echo "<br />";
 echo "  ";

 echo "<br />";
 echo "*********************";
     $this->count --; 
 shuffle ($this->numbers);
 $temp1= 0;
 $temp = 0;
 }
 }
}

$object2 = new Game("Chris", 36);
echo $object2->checkAge();
echo $object2->display(3);




?>

 

 

Link to comment
Share on other sites

Hi, yeah I did see your reply last time, sorry for not thanking you, only problem is your code is quite different from mine, and at present because I am learning I don’t want to simply cut and paste someone else’s code.  Since I started this thread I have learnt that the explode() is for strings only and not numbers but there is a workaround.  Do you know how I could explode whole numbers into an array, but for instance if the numbers are 1, 15, 25, 28, I want them placed in an array in that format, not splitting them up into single integers?

Link to comment
Share on other sites

only problem is your code is quite different from mine, and at present because I am learning I don’t want to simply cut and paste someone else’s code

That fine. I just through you may have forgotten about your previous post as you didn't reply to it last time.

 

I want them placed in an array in that format, not splitting them up into single integers?

If you want to add a string of numbers, eg

$numbers = '1, 3, 18, 27';

And you want to add them into an array you can simply do

$array[] = $numbers;

That will add the string of numbers to the array.

Link to comment
Share on other sites

The only places you use explode on are lines 58 and 69

58.	  $temp = explode(' ', $this->numbers[$j]);
59.	  for ($a =0; $a <$temp.length; $j++);
...
69.	  $temp1 = explode(' ', $this->numbers[$j]);
70.	  for ($a =0; $a <$temp1.length; $j++);

On lines 58 and 69 you use explode to get the individual numbers from $this->numbers[$j]. But you are not doing anything with them afterwards (lines 59 and 70). The for loop is not constructed incorrectly. Variables do not have properties such as .length.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.