Jump to content

2 Arrays in one! CraZY!!


Monkuar

Recommended Posts

$winning_numbers = explode("|", "8|18|3,8|18|3");

			while($ticket = $DB->fetch_row($query)) {
				$correct_numbers = 0;
				$correct = 0;
				$numbers_chosen = @explode("|","8|18|3,8|18|3");
				$your_numbers = array();
				$array1 = array_count_values($numbers_chosen);
				$array2 = array_count_values($winning_numbers);
				foreach($array1 as $number1 => $count1)
				{
				foreach($array2 as $number2 => $count2)
				{
				if($number2 == $number1 and $count2==$count1)
				$correct_numbers += $count2;
				}
				}
				$use = 0;
				echo $correct_numbers;
				echo "<br>";

 

As you can see, my $correct_numbers variable will spit out "3" because it matches 3 each INDIVIDUAL numbers from $numbers_chosen in the arrays.

 

Now my problem is.  This code wasn't  supposed to take in a "," to.  So essentially This should display "6" because I want it to read through each , as a new Array/Group if you will.

 

So let's say if I changed it to:

 

$winning_numbers = explode("|", "2|18|3,8|18|2");

$numbers_chosen = @explode("|","8|18|3,8|18|3");

 

It would echo out "4" As it only matches 4 of them.

 

Once I know how to do this, I will have a full functioning lottery System on my site, I am very anxious for replies, Hope you can Help.  Thank you.

 

 

 

 

TLDR??:

I want $correct_numbers to beable to count arrays with the comma's to

 

Link to comment
Share on other sites

Something like this?

 

<?PHP

  //### Set correct numbers to 0
  $correct_numbers = 0;

  //### Assign the chosen numbers and winning numbers
  $winning_numbers_str = "2|18|3,8|18|2";
  $numbers_chosen_str  = "8|18|3,8|18|1";
  
  //### Match all numbers from the numbers string
  preg_match_all('#([0-9]{1,})#',$winning_numbers_str, $winning_numbers);
  preg_match_all('#([0-9]{1,})#',$numbers_chosen_str, $numbers_chosen);
  
  //### Assign the numbers to the associated variable
  $winning_numbers = $winning_numbers[0];
  $numbers_chosen  = $numbers_chosen[0];
  
  //### Compare each value from both arrays
  foreach($winning_numbers AS $key => $number) {
    if(isSet($numbers_chosen[$key]) && $numbers_chosen[$key] == $number) {
      $correct_numbers++;
    }
  }
  
  //### Display the output of the arrays and correct number count
  print_r($winning_numbers);
  echo '<br>';
  print_r($numbers_chosen);
  echo '<br>';
  echo 'Total Correct Numbers: '.$correct_numbers;
  ?>

 

Try it out and tell me how it goes :)

 

Regards, PaulRyan.

Link to comment
Share on other sites

Something like this?

 

<?PHP

  //### Set correct numbers to 0
  $correct_numbers = 0;

  //### Assign the chosen numbers and winning numbers
  $winning_numbers_str = "2|18|3,8|18|2";
  $numbers_chosen_str  = "8|18|3,8|18|1";
  
  //### Match all numbers from the numbers string
  preg_match_all('#([0-9]{1,})#',$winning_numbers_str, $winning_numbers);
  preg_match_all('#([0-9]{1,})#',$numbers_chosen_str, $numbers_chosen);
  
  //### Assign the numbers to the associated variable
  $winning_numbers = $winning_numbers[0];
  $numbers_chosen  = $numbers_chosen[0];
  
  //### Compare each value from both arrays
  foreach($winning_numbers AS $key => $number) {
    if(isSet($numbers_chosen[$key]) && $numbers_chosen[$key] == $number) {
      $correct_numbers++;
    }
  }
  
  //### Display the output of the arrays and correct number count
  print_r($winning_numbers);
  echo '<br>';
  print_r($numbers_chosen);
  echo '<br>';
  echo 'Total Correct Numbers: '.$correct_numbers;
  ?>

 

Try it out and tell me how it goes :)

 

Regards, PaulRyan.

 

Extremely sorry I must have worded it wrong. lol :(

 

 

The $winning_numbers_str must only be like this:

  //### Assign the chosen numbers and winning numbers
  $winning_numbers_str = "8|18|3";
  $numbers_chosen_str  = "8|18|3,8|18|3";

 

Because the jackpot for the lottery is only 3 balls ( 8|18|3 ) That need to match with numbers_chosen.

 

So essentially it's not really a "Match" as before? More of a array Match if you will instead? (Kinda of confusing for me)

 

That above code should spit out 6 ? Right now it's spitting out 3 only :(

 

 

Link to comment
Share on other sites

You specifically gave this to us in your original post.

$winning_numbers = explode("|", "2|18|3,8|18|2");
$numbers_chosen = @explode("|","8|18|3,8|18|3");

So that is what I worked with...

 

Try this out instead:

<?PHP

  //### Set correct numbers to 0
  $correct_numbers = 0;

  $winning_numbers    = explode("|", "8|18|3");
  $numbers_chosen_str = "8|18|3,8|18|3";
  
  //### If chosen numbers are multiple "arrays" use the if
  if(strstr($numbers_chosen_str,",")) {
  
    //### Explode the multiple "arrays"
    $numbers_chosen_arr = explode(",",$numbers_chosen_str);
    
    //### Iterate through each "array"
    foreach($numbers_chosen_arr AS $key => $numbers) {
    
      //### Explode the "array"
      $number_chosen = explode("|",$numbers);
      
      //### Iterate through each array element
      foreach($number_chosen AS $key => $number) {
      
        //### If the array element is a winning number, the number is correct
        if($winning_numbers[$key] == $number) {
          $correct_numbers++;
        }
      }
    }
    
  //### If chosen numbers is a single "array" use else
  } else {
  
    //### Explode the single "array"
    $numbers_chosen = explode("|",$numbers_chosen_str);
    
    //### Iterate through each array element
    foreach($number_chosen AS $key => $number) {
    
      //### If the array element is a winning number, the number is correct
      if($winning_numbers[$key] == $number) {
        $correct_numbers++;
      }
    }
  }
  
  echo 'Total Correct Numbers: '.$correct_numbers;

  ?>

 

Regards, PaulRyan.

 

 

Link to comment
Share on other sites

You specifically gave this to us in your original post.

$winning_numbers = explode("|", "2|18|3,8|18|2");
$numbers_chosen = @explode("|","8|18|3,8|18|3");

So that is what I worked with...

 

Try this out instead:

<?PHP

  //### Set correct numbers to 0
  $correct_numbers = 0;

  $winning_numbers    = explode("|", "8|18|3");
  $numbers_chosen_str = "8|18|3,8|18|3";
  
  //### If chosen numbers are multiple "arrays" use the if
  if(strstr($numbers_chosen_str,",")) {
  
    //### Explode the multiple "arrays"
    $numbers_chosen_arr = explode(",",$numbers_chosen_str);
    
    //### Iterate through each "array"
    foreach($numbers_chosen_arr AS $key => $numbers) {
    
      //### Explode the "array"
      $number_chosen = explode("|",$numbers);
      
      //### Iterate through each array element
      foreach($number_chosen AS $key => $number) {
      
        //### If the array element is a winning number, the number is correct
        if($winning_numbers[$key] == $number) {
          $correct_numbers++;
        }
      }
    }
    
  //### If chosen numbers is a single "array" use else
  } else {
  
    //### Explode the single "array"
    $numbers_chosen = explode("|",$numbers_chosen_str);
    
    //### Iterate through each array element
    foreach($number_chosen AS $key => $number) {
    
      //### If the array element is a winning number, the number is correct
      if($winning_numbers[$key] == $number) {
        $correct_numbers++;
      }
    }
  }
  
  echo 'Total Correct Numbers: '.$correct_numbers;

  ?>

 

Regards, PaulRyan.

 

Don't know what else to say, I am speechless you actually wrote the comments out for me that greatly helped, do you use a php design software? (I use notepad++) Should i switch to a PHP Software program does it make it easier tolearn?

 

I will pm you the lottery page once im done editing style/etc this is just wonderful! here is a beta screenshot of it, users can choose alot of balls/tickets.

 

30507127.jpg

 

I think the only security flaw I have left is if people use Tamper Data to try to insert 2|1  instead of required x|x|x 3 balls.  I have security in place to protect for "is_numeric" and max > 36 = error out/etc, just 1 more security flaw I think is when people will try to tamper data and input x|x instead of the fully x|x|x  or if they will try to do things like x|x|x,,x|x,x| or something, which is not matching to x|x|x,x|x|x which I will post more help on that later after college class.

 

Thanks

 

I dont want to sound homo but I love you

 

 

 

 

EDIT:

 

If I just use 1 Array

$numbers_chosen_str = "8|18|3";

Warning: Invalid argument supplied for foreach() in C:\wamp\www\sources\lottery.php on line 74

 

Hmm weird.

 

Sorry to be Picky, but how would I go about finding the highest first match first and use it in a variable out of all the arrays?

Link to comment
Share on other sites

What you are asking for makes absolutely no sense based upon the ultimate goal. You are allowing the user to submit multiple picks (3 numbers each) for a lottery draw. You wouldn't count how many matches there are across all the picks - you need to know how many matches there are in each single pick.

 

Let's say the winning numbers are 3, 8, 18

 

If a user has two picks of 8, 18, 20 AND 5, 8, 18 there are four total matches. But, that really doesn't tell you if either pick was a winner or not. For example, what if the user's two picks are 3, 8, 18 AND 5, 8, 12. Again, there are four matches total for both picks. But, the first pick was an exact match. Plus, you have said nothing about whether the position of the numbers is relevant to a match or not.

 

Besides you are making this way more difficult than it needs to be. The data should already be in arrays rather than a string. There are many array functions that can be used without having to loop through each individual record. The following will do EXACTLY as you requested - even though I don't think it is what you really want. It will return the number of matches across ALL the user picks (whether that is one pick or many) for a single winning draw. This code does NOT consider the positions of the numbers in the picks/draw for the purposes of a match.

 

function getMatches($draw_pick_str, $user_picks_str)
{
    $matches = 0;

    //Convert data into arrays and process
    $draw_pick_ary = explode('|', $draw_pick_str);
    $user_picks_ary = explode(',', $user_picks_str);
    foreach($user_picks_ary as $user_pick_str)
    {
        $user_pick_ary = explode('|', $user_pick_str);
        $matches += count(array_intersect($draw_pick_ary, $user_pick_ary));
    }

    return $matches;
}

$winning_numbers    = "2|18|3";
$numbers_chosen_str = "8|18|3,5|2|3";

echo getMatches($winning_numbers, $numbers_chosen_str);
//Output: 4

Link to comment
Share on other sites

Psycho has made a few valid points, but as always I adhere to the posters request :)

 

This line within the ELSE statement:

foreach($number_chosen AS $key => $number) {

Change it too:

foreach($numbers_chosen AS $key => $number) {

 

PHP Design Software - I was using Notepad until recently and the looked at and began to use Notepad 2 http://www.flos-freeware.ch/notepad2.html - I like the simplicity of it all :)

 

Regards, PaulRyan.

Link to comment
Share on other sites

Hey  Psycho, your code works fine.

 

 

I agree with you, the problem lies in my details of explaing what exactly I am looking for and you're correct about the matches, Because what if the winning balls are

3|2|1

 

and let's say somone picked these balls for their Lottery ticket:

 

1|3|2,3|2|2,3|2|1

 

 

I guess what I need is something that will select the highest Array count out of the initial array and use it as a variable.

 

Because then according to my numbers above, that user would win a jackpot, but not technically because he never matched the 3|2|1 , only matched the singular arrays.  Again, I am sorry for not explaining myself better.  I hope I did now though.

 

 

To generate if 1 of my Members one a Jackpot, I would simply use a

if($correct_numbers == 3)
/etc/etc do ifs/elses/functions/etc/

 

But like I said above, would be a ghetto way to win a jackpot if that was a case, I guess I am looking to match the first highest array count.  If that makes any sense, sorry for the inconvenience.

 

Awww I feel bad now

 

Oh nice, does that notepad2 bring up functions while u type/etc?

Link to comment
Share on other sites

You are doing a very poor job of explaining your needs and/or you really don't have a solid understanding of what you are exactly doing.

 

Based upon your last comments a user's picks is only a jackpot winner if they have a pick that has the same numbers - in the same order. But, do you have other types of winners based upon some matching numbers and some matching positions? You really need to be comparing each user pick individually. But, if you only have one type of winner (jackpot) and you just want something to give the user some info regarding how close they were to winning, then what you are requesting kinda makes sense. I would probably create a function to return "true" if one of the picks was an exact match otherwise return a number of the pick with the most matches.

 

But, I am reluctant to provide any more code without some better explanation of what you think you are doing.

Link to comment
Share on other sites

You are doing a very poor job of explaining your needs and/or you really don't have a solid understanding of what you are exactly doing.

 

Based upon your last comments a user's picks is only a jackpot winner if they have a pick that has the same numbers - in the same order. But, do you have other types of winners based upon some matching numbers and some matching positions? You really need to be comparing each user pick individually. But, if you only have one type of winner (jackpot) and you just want something to give the user some info regarding how close they were to winning, then what you are requesting kinda makes sense. I would probably create a function to return "true" if one of the picks was an exact match otherwise return a number of the pick with the most matches.

 

But, I am reluctant to provide any more code without some better explanation of what you think you are doing.

 

Dunno if I can explain it any better man...

 

Here I will show pictures/etc to get a better explanation since my english is poor  :'(

 

 

 

 

lol1vi.png

lol2ia.png

 

Pretty much the correct_balls somehow need to match any of the numbers_chosed field, and I need the array to return a "true" if it finds that 8|13|3 pattern inside the numbers_chosed field (shown above)

 

Now the hard part is for me needing to securely check the input when they choose there BALLS to not select the same one ( wouldn't matter because if the function you make will return true on the first find) then the numbers_chosed could have a multiple of different 8|18|3's in it, but it only returns back true once, and when it returns back one it will run the jackpot CODE that I have to give that user (XXXX AMOUNT OF FORUM GOLD) from the jackpot(Which I will code soon, really easy just I need to add it to increment per ticket cost/etc) [More users buy tickets  = higher jackpot ] I will do that once I get this out of the way.

 

The problem I think that is confusing you and me, is that trying to find "matches" would perform a "LOOSE" way of trying to get numbers, which would make the user have a way easier chance to win forum Gold, let's just scratch that. 

 

Ultimately, I really just need it to beable to match any 2 balls out of 8|18|3 to win a small amount (so the user has feels like he's actually winning forum gold while purchasing tickets), but then if they MATCH all 3 amounts, return a different variable to True, to just run the jackpot code on that specific Member and give him all the jackpot forum Gold.  This is my ultimate goal and I believe I am very close to it, I have finalized the POST DATA (securing it)/etc, now I just need help on to get the data correctly identified through arrays/such which just give me a hard time.

 

Hope this helps.

 

And I guess when I mean "match" it needs to be in the same x|x|x  ticket (group)

Link to comment
Share on other sites

OK, if I understand you correctly, the player's picks only need to match the winning pic number - regardless of order. Also, you say you do want to give the players a smaller payout if they match two numbers. But, we are back at a problem.

 

What if a user has 10 picks and 3 of those picks match 2 numbers? Shouldn't that user get the small payout multiplied by 3. The problem is you are trying to count the matches across ALL the picks. Instead you need to do the matches for each pick individually. I still believe you have not worked out all the logic of how you want this to operate and are trying to do it as you go.

 

But, here is an example of a possible solution based upon some rules I will set.

- Winning draw will consist of three random numbers

- Users may purchase as many "picks" as they want (consisting of three different numbers)

- Winning:

    - A match of any two numbers will win 5 units

    - A match of all three numbers will win 100 units

 

Based upon those rules I would create a function to return the combined results of all the user submissions: how many matched 0, how many matched 1, etc. THEN create another process to determine the winnings. Building upon the code I previously posted here is an example script

function getResults($draw_pick_str, $user_picks_str)
{
    $resultsAry = array(0=>0, 1=>0, 2=>0, 3=>0);

    //Convert data into arrays and process
    $draw_pick_ary = explode('|', $draw_pick_str);
    $user_picks_ary = explode(',', $user_picks_str);
    foreach($user_picks_ary as $user_pick_str)
    {
        $user_pick_ary = explode('|', $user_pick_str);
        $matches = count(array_intersect($draw_pick_ary, $user_pick_ary));
        $resultsAry[$matches]++;
    }

    return $resultsAry;
}


$winning_numbers = "2|18|3";
$numbers_chosen = "8|18|3,8|18|3,5|8|12,1|18|22";
$payouts = array(0, 0, 5, 100);

$results = getResults($winning_numbers, $numbers_chosen);
$total_winnings = 0;
foreach($results as $matches => $count)
{
    $winnings = $payouts[$matches] * $count;
    $total_winnings += $winnings;
    echo "You had {$count} picks that matched {$matches} numbers, winning {$winnings} gold.<br>\n";
}
echo "<br>Your total winnings are {$total_winnings} gold.";

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.