0

I'm making a filter function, and I was wondering if there is a way to do the following comparison: The user wants to see some tests, and each of these tests has an array of 0/1, the user only needs to see tests with array equal to the once they made. Just for info 0=passed and `1=Failed.

The array made my user

array(12) { [0]=> string(1) "0" [1]=> string(1) "0" [2]=> string(1) "0" [3]=> string(1) "0" [4]=> string(1) "0" [5]=> string(1) "0" [6]=> string(1) "0" [7]=> string(1) "0" [8]=> string(1) "0" [9]=> string(1) "0" [10]=> string(1) "1" [11]=> string(1) "1" }

Random test array to compare with

array(30) { [0]=> string(6) "passed" [1]=> string(6) "passed" [2]=> string(6) "passed" [3]=> string(6) "passed" [4]=> string(6) "passed" [5]=> string(6) "passed" [6]=> string(6) "passed" [7]=> string(6) "passed" [8]=> string(6) "passed" [9]=> string(6) "passed" [10]=> string(6) "passed" [11]=> string(6) "passed" [12]=> string(6) "passed" [13]=> string(6) "passed" [14]=> string(6) "passed" [15]=> string(6) "passed" [16]=> string(6) "passed" [17]=> string(6) "passed" [18]=> string(6) "passed" [19]=> string(6) "passed" [20]=> string(6) "passed" [21]=> string(6) "passed" [22]=> string(6) "passed" [23]=> string(6) "passed" [24]=> string(6) "passed" [25]=> string(6) "passed" [26]=> string(6) "passed" [27]=> string(6) "passed" [28]=> string(6) "passed" [29]=> string(6) "passed" }

The users array is 12 long, and then I was wondering if you could take this array and see if it appears in the large array(30). Lets say the first 0-11 places in the large array is equal to the users, then it doesn't check, and goes on to the nest test.

Not working code example:

for($i = 0; $i < sizeof($LargeArray; $i++)
{
  if($LargeArray['test'.$i][$i] == $UserArray)
  {
    echo"$LargeArray['test'.$i]";
    echo"is equal to users array.";
    exit(); //should stop checking this test1 and go check test2
  }
}

How can i check if users array(12) is equal to the first 0-11 in largeArray()? and if not then 1-12.... 18-30.

0

2 Answers 2

1

I think casting those arrays to strings and then using strpos() sounds like the a good way to confirm if the larger one contains the smaller one:

$reference = array_values([0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0]);
$user      = array_values([0,0,0,0,0,0,0,0,0,0]);

$referenceString = implode(',', $reference);
$userString = implode(',', $user);

$matchStart = strpos($referenceString, $userString);
if ($matchStart === false) {
    // no match
}

// Starting offset in original array
echo $matchStart / 2;

The array_values is only there to ensure you have a numeric index. This will easily allow you to convert this index back to the original key by matching it with the key name using array_keys().

Sign up to request clarification or add additional context in comments.

Comments

0

Not tested, but first, get the arrays to have the same types of values. Maybe change the $UserArray to use the same strings as $LargeArray:

$user = array_map(function($v) { return $v ? 'passed' : 'failed'; }, $UserArray);

Then loop and slice 12 from $LargeArray and compare with $UserArray:

$i = 0;
while($test = array_slice($LargeArray, $i, 12)) {
    if($test === $user)) {
        //equal
    } else {
        //not equal
    }
    $i += 12;
}

The fourth parameter to array_slice is bool $preserve_keys = FALSE so the slice will be a 0 based index like $UserArray.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.