see if 3 or more numbers match an array - php

Hay i have an array consisting for 6 random numbers, here's an example
[4,8,12,22,23,43]
I also have 100 arrays containing 6 numbers, these are all random a few examples could be
[5,8,15,47,32,48]
[3,4,8,12,33,42]
[8,12,26,55,43,33]
[4,63,45,23,45,55] ...
I want to see how many times (out of the array of 100) these numbers match at least 3 from the top array. As you can guess this is a lottery experiment.
As you can see array number 3 matches 3 numbers from the top array.
Any ideas how do do this? With perhaps an option to see if 4 numbers matched.

$master_array = array(4, 8, 12, 22, 23, 43);
$arrays = array(array(5, 8, 15, 47, 32, 48),
array(3, 4, 8, 12, 33, 42),
array(8, 12, 26, 55, 43, 33),
array(4, 63, 45, 23, 45, 55));
foreach ($arrays as $arr)
{
$intersect = array_intersect($master_array, $arr);
if (count($intersect)==3) print 'Match: '.print_r($arr, true).PHP_EOL;
}

maybe smth like this:
$winner = [4,8,12,22,23,43];
$arrays = //all your 100 arrays
$i = 0; // number of matches
foreach ($arrays as $array)
{
$result = array_intersect($array, $winner);
if (count($result) >= 3) $i++;
}

Related

Creating a list and indexing

Given a two list. Create a third list by picking an odd-index element from the first list and even index elements from the second.
For Example:
listOne = [3, 6, 9, 12, 15, 18, 21]
listTwo = [4, 8, 12, 16, 20, 24, 28]
Expected Output:
Printing Final third list
[6, 12, 18, 4, 12, 20, 28]
I think, it will be helpful for you.
<?php
$listOne = [3, 6, 9, 12, 15, 18, 21];
$listTwo = [4, 8, 12, 16, 20, 24, 28];
$NlistOne=[];
$NlistTwo=[];
//odd-index positions from list one [6, 12, 18]
foreach ($listOne as $key => $value) {
if($key%2==1){
array_push($NlistOne, $value);
}
}
//even-index positions from list two [4, 12, 20, 28]
foreach ($listTwo as $key => $value) {
if($key%2==0){
array_push($NlistTwo, $value);
}
}
//Printing Final third list [6, 12, 18, 4, 12, 20, 28]
print_r(array_merge($NlistOne,$NlistTwo));
?>
Output will be:
Array ( [0] => 6 [1] => 12 [2] => 18 [3] => 4 [4] => 12 [5] => 20 [6] => 28 )
//init result array
//loop over listOne, using for($i=1;$i<sizeof($listOne);$i=$i+2)
//and add to result for each iteration, $resultArr[] = $listOne[$i]
//do the same with listTwo, but for($i=*0*
You can merge both of arrays and then pick all odd elements
$listOne = [3, 6, 9, 12, 15, 18, 21];
$listTwo = [4, 8, 12, 16, 20, 24, 28];
$result = [];
foreach ( array_merge($listOne, $listTwo) as $value ){
if ( $key % 2 ) {
$result[] = $value;
}
}
If array length isn't fixed, say it could contain not 7 elements, then you need to check it before merging to make it have odd number of elements
$listOne = [3, 6, 9, 12, 15, 18, 21, 777];
$listTwo = [4, 8, 12, 16, 20, 24, 28];
$result = [];
if ( count($listOne) % 2 !== 1 ) {
$listOne[] = 0;
}
foreach( array_merge($listOne, $listTwo) as $value ){
if ( $key % 2 ) {
$result[] = $value;
}
}
you don't have to loop over whole array array_filter will do this for you, Constant ARRAY_FILTER_USE_KEY will check each key for odd or for even
<?php
$a1 = [3, 6, 9, 12, 15, 18, 21];
$a2 = [4, 8, 12, 16, 20, 24, 28];
function odd($var)
{
// returns whether the input integer is odd
return $var & 1;
}
function even($var)
{
// returns whether the input integer is even
return !($var & 1);
}
$result= (array_merge(array_filter($a1, 'odd', ARRAY_FILTER_USE_KEY),array_filter($a2, 'even', ARRAY_FILTER_USE_KEY)));
output you will get
Array (
[0] => 6
[1] => 12
[2] => 18
[3] => 4
[4] => 12
[5] => 20
[6] => 28 )
Iterate over first one and take only values of odds indices, and loop again through second one and take evens indices.
$listOne = [3, 6, 9, 12, 15, 18, 21];
$listTwo = [4, 8, 12, 16, 20, 24, 28];
$res = [];
for ($i=0; $i < count($listOne); $i++) {
if($i & 1) // $i is odd.
$res[] = $listOne[$i];
}
for ($j=0; $j < count($listTwo); $j++) {
if(n % 2 === 0) // $j is even.
$res[] = $listTwo[$j];
}
Result:
echo "List One :".json_encode($listOne)."<br>";
echo "List Two :".json_encode($listTwo)."<br>";
echo "Lists Merged:".json_encode($res);
Output:*
/*
List One :[3,6,9,12,15,18,21]
List Two :[4,8,12,16,20,24,28]
Lists Merged:[6,12,18,4,12,20,28]
*/
Iterate over arrays:
take odds in array starting with index One and increment it by two.
take evens in array by starting with index Zero and increment it by two.
$listOne = [3, 6, 9, 12, 15, 18, 21]; $listTwo = [4, 8, 12, 16, 20, 24, 28];
$res = [];
for ($i=1; $i < count($listOne); $i+=2) {
$res[] = $listOne[$i];
}
for ($j=0; $j < count($listTwo); $j+=2) {
$res[] = $listTwo[$j];
}
print(json_encode($res)); // [6,12,18,4,12,20,28]

For-each loop, larger than 20 into another array

I'm stuck on this exercise that I can't seem to get for the life of me.
$numbers2 = [21, 5, 4, 6, 76, 2, 18, 85, 55, 1];
foreach ($numbers2 as &$value) {
$largeNumbers[] = $value > 20;
}
That's the code I have so far. What I am trying to do here is use a for-each loop to add all the numbers that are larger than 20 into another Array, which I have named $largeNumbers. The code I have inserted above seems to be printing out true and false values, which is not what I was going for. I'd really appreciate it someone could tell me what I'm possibly doing wrong or even show me a better way. I have to use a for-each loop.
For each item, you are checking if it's larger than 20, which results in a boolean value (it either is or is not), and you then store this value to the result array. Instead, you could use an if statement` to only take the elements that answer the condition:
foreach ($numbers2 as &$value) {
if ($value > 20) {
$largeNumbers[] = $value;
}
}
<?php
$nums = [21, 5, 4, 6, 76, 2, 18, 85, 55, 1];
$less_than_or_equal_to_20 = [];
foreach($nums as $v)
if($v <= 20)
$less_than_or_equal_to_20[] = $v;
$out = array_diff($nums, $less_than_or_equal_to_20);
var_export($out);
Output:
array ( 0 => 21, 4 => 76, 7 => 85, 8 => 55, )
<?php
$nums = [21, 5, 4, 6, 76, 2, 18, 85, 55, 1];
foreach([$nums] as $v)
$out = array_filter($v, function($v) {
return $v > 20;
});
var_export($out);
Output:
array ( 0 => 21, 4 => 76, 7 => 85, 8 => 55, )

PHP Find a maximum average for 10 subsequent numbers in a list of 50 random numbers

I'm working on this for quite a while, but I don't know how to fix this:
I have a list of 50 random numbers, and when 10 subsequent numbers from this this (numbers 11-20 for example, or numbers 24-33) reach an average of x, I want to get a notification.
The 50 numbers are in 1 row of a (HTML) table, each in a different column.
Anybody with an idea how to start? Thanks!
If you have your numbers in an array, you can loop through the array in chunks of 10, and then find the maximum average.
<?php
$numbers = array(1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1,
10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 11, 12, //large numbers here
1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1);
$number = 10; //numbers in a set
$max = 0;
$index = 0;
$size = sizeof($numbers) - $number;
for ($i = 0; $i < $size; $i++) {
$tmp = array_sum(array_slice($numbers, $i, $number)) / $number;
if ($tmp > $max) {
$max = $tmp;
$index = $i;
}
}
echo "Largest set of " . $number . " numbers is: " . implode(', ', array_slice($numbers, $index, $number)) . "\nAverage of: " . $max;
Output:
Largest set of 10 numbers is: 10, 11, 12, 13, 14, 15, 14, 13, 12, 11
Average of: 12.5
You can then compare the largest average to your threshold and notify yourself.

Create rows of elements based on an array of length values

Is there a neat way to split an array into chunks based on an array of lengths?
Input:
$start = range(0, 30);
$length = [3, 7, 2, 12, 6];
Desired Output:
[
[0, 1, 2], // 3
[4, 5, 6, 7, 8, 9, 10], // 7
[11, 12], // 2
[13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], // 12
[25, 26, 27, 28, 29, 30], // 6
];
Using array_splice:
$target = array(); // or use [] in PHP 5.4
foreach($length as $i) {
$target[] = array_splice($start, 0, $i);
}
Try it.
Be advised, this changes $start!
This is very easily accomplished with the following:
Code:
$target = array();
$offset = 0;
foreach ($length as $lengthValue) {
$target[] = array_slice($start, $offset, $lengthValue);
$offset += $lengthValue;
}
var_dump($target);
Explanation:
What you are doing here is using the array_slice() method (very similar to the substr) to extract the portion of the array and then inserting it into the target array. Incrementing the offset each time allows the function to remember which one to use next time.

Merging of array items

I have already asked this question once for a solution in Python:
python intersect of dict items
but I am now looking for the equivalent solution in PHP. Specifically I am using CakePHP 2.0 on the ConsoleShell.
I have an undetermined number of nests in an array and I want to loop over them like this:
$array = array(1 => array(1, 2, 3, 4), 2 => array(7, 8), n => array(n1, n2, n3))
foreach($array[1] as $k1 => $v1) {
foreach($array[2] as $k2 => $v2) {
foreach($array[n] as $kn => $vn) {
// do something with this combination
}
}
}
Bear in mind that n can be any number (the total number of nests), so I need code that allows for a dynamic number of nests.
Any ideas please?
Thanks
EDIT:
Just to clarify what I am after, if
$array = array(array(0, 1, 2, 3), array(0, 6, 7), array(0, 9, 10));
I want to end up with:
$array2 = array(
array(0, 0, 0),
array(0, 0, 9),
array(0, 0, 10),
array(0, 6, 0),
array(0, 6, 9),
...
array(3, 6, 10),
array(3, 7, 0),
array(3, 7, 9),
array(3, 7, 10)
);
however if $array has 4 array elements, then $array2 should have 4 elements on each line etc.
function dosomething($a) {
if (is_array($a))
foreach ($a as $aa)
dosomething($aa)
else {
//do something
}
}
Edit:
After the OQ was updated it became clear, that not the depth is variable, but the element count. So we do not need recursion, but nested foreaches
This gives the output as requested in the OQ
<?php
$array = array(array(0, 1, 2, 3), array(0, 6, 7), array(0, 9, 10));
$result=array(array());
foreach ($array as $a) {
$oldresult=$result;
$result=array();
foreach ($oldresult as $or) {
foreach($a as $aa) {
$nr=$or;
$nr[]=$aa;
$result[]=$nr;
}
}
}
print_r($result);
?>
In short: We start with a result array, that has one element: an empty array. Now we cycle through the first level of the input array, in every step we replace all elements of the result array with a series of elements consiting of the original element with each of the members of our current array attached.
The first iteration replaces
array() with array(0), array(1), array(2), array(3)
The second iteration replaces
array(0) with array(0,0), array(0,6), array(0,7)
array(1) with array(1,0), array(1,6), array(1,7)
...
The third iteration replaces
array(0,0) with array(0,0,0), array(0,0,9), array(0,0,10)
array(0,1) with array(0,1,0), array(0,1,9), array(0,1,10)
...
array(1,0) with array(1,0,0), array(1,0,9), array(1,0,10)
...

Categories