1

I have two arrays:

$a = array("w","c","d","e","g","h");
$b = array("c","d","e");
$c = array("c","e","d");

It should return "TRUE" if we check if $a contain $b or $a contain $c because the index of $b are in sequence with $a & $c i.e.they all have $ "c","d","e" and they come in a sequence in $a. However, if $b = array("c", "g", "d"), then we should get false because it has all the elements but they are not in correct sequence in $a.

1
  • Tried anything? Commented Feb 28, 2019 at 22:34

1 Answer 1

1

Something like this should do the trick:

$a = ["w","c","d","e","g","h"];
$b = ["c","d","e"];
$c = ["c","e","d"];
function containsSequence($arr, $subArray){
    $keys = array_keys($arr, $subArray[array_keys($subArray)[0]]); 
    foreach($keys as $k) {
        if(array_slice($arr, $k, count($subArray)) == $subArray){
            return true;    
        }
    }
    return false;
}
echo 'Contains sequence: '.(containsSequence($a,$b)?'yes:':'no');
echo 'Contains sequence: '.(containsSequence($a,$c)?'ues':'no');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.