Home > Net >  check if the sequence of values in subarray exist in an array using where in Laravel
check if the sequence of values in subarray exist in an array using where in Laravel

Time:09-22

Example: if i have two array saved in database in different records $array1 = [1,2,3,4,5,6]; $array2 = [6,5,4,3,2,1];

and subArray $subArray = [1,2,3];

and i want to get the array where the sequence of $subArray exists using query in laravel which will $array1

CodePudding user response:

Here is a possible solution:

  1. Get intersecting keys
  2. Iterate on keys and slice it
    function isSequence($array, $subArray){
        $keys = array_keys($array, $subArray[array_keys($subArray)[0]]); 
        foreach($keys as $key) {
            if(array_slice($array, $key, count($subArray)) == $subArray){
                return true;    
            }
        }
        return false;
    }

SANDBOX

  • Related