Detecting arrays with enough entries in common [closed]

Posted on


I have made a function called testing. This function tests whether five numbers from each sub-array of $source are included in $dataArray sub-arrays, at least $limit times. If so, it returns the source sub-array as a string; else it returns “x”. Please see or try the example below if this description isn’t clear.

And now it comes: this function works properly but with large quantity of sub-arrays in $sourceArray it is relatively slow (13 mins for 100000 tests on i7-7700K, PHP/7.1.4).

Is there any way to make this function more effective and quicker? Is there any noticeable bad construction / leak which may have an immediate impact on performance?


// and so on - circa 4000 sub-arrays

// and so on - 10M+ sub-arrays (variable quantity)

function testing($dataArray, $source, $limit) {

$guessedArray = array();
foreach ($dataArray as $data) {
    $counter = 0;
    foreach ($source as $number) {
        if (in_array($number, $data)) {
    array_push($guessedArray, $counter);

$guessedArray = array_count_values($guessedArray);

if ($guessedArray[5] >= $limit) {
    return implode("-", $source);
  } else {return "x";}

foreach ($sourceArray as $oneArray) { // just example but $sourceArray's sub-arrays must be processed one by one
$result = testing($dataArray, $oneArray, 1);
echo "$result<br />";


-> because only the third sub-array in $sourceArray contains five numbers located in $dataArray's sub-arrays for at least $limit times
(in this example $limit = 1)



This testing routine runs three to four times faster on the test data:

function testing($dataArray,$source,$limit)
  $matchesFound = 0;
  foreach ($dataArray as $data)
    $counter = 0;
    foreach ($data as $number)
      if (!in_array($number,$source)) $counter++;
      if ($counter == 2) break;
    if ($counter == 1) $matchesFound++;
    if ($matchesFound >= $limit) return implode("-", $source);
  return "x";

A big assumption is that $dataArray only contains arrays of 6 numbers and you will always look for 5 matching numbers.

The improvement is that I stop looping as soon as I know whether there is a match or not. You simply loop over all the data. So the real speed improvement depends heavily on the data. My guess is that it can perform even better on the complete data set.

More can probably be gained if your question was more informative.

Leave a Reply

Your email address will not be published. Required fields are marked *