How to check if an array has the value one after another in other array PHP?

Viewed 337

I have been a facing a problem in my project. Here is the case

Suppose I have two arrays, array_1, array_2. array_1 has value of ['A', 'B', 'C', 'D', 'E', 'F'] and array_2 has value of ['B', 'C', 'D']. I want the result to true if array_1 has the same value of array_2 one after another. For the above case is true. Now if array_2 has a value of ['C', 'D', 'B'], Then it returns to false because the value is not one after another. Here is the code what I tried

    if (!array_diff($array_2, $array_1)) {
        return true;
    }
return false

But this does not return what I want to do. Thanks in advance

4 Answers

Edit 3:

As Nick pointed out, this wouldn't work in some cases. Like $a = ['a-b', 'c']; $b = ['a', 'b'];.

Go see his solution.

This doesn't always work:

$array_1 = ['A', 'B', 'C', 'D', 'E', 'F'];
$array_2 = ['B', 'C', 'D'];

if (str_contains(implode('-', $array_1), implode('-', $array_2))) {
    return true;
}

return false;

Or simply:

return str_contains(implode('-', $array_1), implode('-', $array_2));

I imploded arrays (making them a string and gluing them with -) and checked if the string of $array_2 is a substring of $array_1's string with Laravel's str_contains(haystack, needle)

Here is a function which will robustly check that the values in $array_2 are in $array_1 in the same order, one after the other (i.e. with sequential indexes):

function compare_arrays($array_1, $array_2) {
    $count_1 = count($array_1);
    $count_2 = count($array_2);
    for ($i = 0; $i < $count_1 - $count_2 + 1; $i++) {
        if ($array_1[$i] == $array_2[0]) {
            for ($j = 1; $j < $count_2 && $i + $j < $count_1; $j++) {
                if ($array_1[$i+$j] != $array_2[$j]) break;
            }
            if ($j == $count_2) return true;
        }
    }
    return false;
}
$array_1 = ['A', 'B', 'C', 'D', 'E', 'F'];
$array_2 = ['B', 'C', 'D'];
echo compare_arrays($array_1, $array_2) ? "true\n" : "false\n";
$array_2 = ['C', 'D', 'B'];
echo compare_arrays($array_1, $array_2) ? "true\n" : "false\n";
$array_2 = ['B', 'D', 'E'];
echo compare_arrays($array_1, $array_2) ? "true\n" : "false\n";
$array_2 = ['A', 'B', 'C'];
echo compare_arrays($array_1, $array_2) ? "true\n" : "false\n";
$array_1 = ['A', 'C', 'C', 'D', 'E', 'F'];
$array_2 = ['C', 'D', 'E'];
echo compare_arrays($array_1, $array_2) ? "true\n" : "false\n";

Output:

true 
false 
false 
true 
true

Demo on 3v4l.org

Unless I missed something, you can simply do this:

$result = strpos(
  implode(array_map('json_encode', $input1)), 
  implode(array_map('json_encode', $input2))
) !== false;

This basically transforms each input into a string where every value is JSON-encoded, then checks whether the second one is part of the first one.

Demo here: https://3v4l.org/eAQst

I liked this question and I liked the different solutions. I know I'm late to the party but here is my attempt at it. I don't know how it compares in terms of cost with big data sets but this should be quite robust.

My attempt just iterates across each element and then creates a second array of the same size as the searched array and then does a comparison check at the end.

Code:

function checkArray($a, $b){

  $test = FALSE;

  for($i = 0; $i < count($a); $i++){

    for($j = 0; $j < count($b); $j++){

      $array[] = $a[$i + $j];

    }

    if($array == $b){

      $test = TRUE;
      break;

    }

    unset($array);

  }

return $test;

}


$sample = ['A', 'B', 'C', 'D', 'E', 'F'];
$match = ['B', 'C', 'D'];
echo checkArray($sample, $match) ? "true\n" : "false\n"; //True
$match = ['C', 'D', 'B'];
echo checkArray($sample, $match) ? "true\n" : "false\n"; //False
$match = ['B', 'D', 'E'];
echo checkArray($sample, $match) ? "true\n" : "false\n"; //False
$match = ['A', 'B', 'C'];
echo checkArray($sample, $match) ? "true\n" : "false\n"; //True
$match = ['A'];
echo checkArray($sample, $match) ? "true\n" : "false\n"; //True
$match = ['F'];
echo checkArray($sample, $match) ? "true\n" : "false\n";//True
$sample = ['A', 'C', 'C', 'D', 'E', 'F'];
$match = ['C', 'D', 'E'];
echo checkArray($sample, $match) ? "true\n" : "false\n";//True
$sample = ['C', 'D', 'E'];
$match = ['A', 'C', 'C', 'D', 'E', 'F'];
echo checkArray($sample, $match) ? "true\n" : "false\n";//False

This will output:

true
false
false
true
true
true
true
false
Related