3

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

11
  • Use array unique Commented Jan 8, 2019 at 5:42
  • @RameshS I dont think array unique could satisfy my need Commented Jan 8, 2019 at 5:45
  • Do you want unique values from both array? What do you want? Commented Jan 8, 2019 at 5:45
  • Do the arrays have to be the same length or can compare against a fragment of an array so long as the order is the same? Commented Jan 8, 2019 at 5:47
  • First use array_intersect on array_1 and then use this solution stackoverflow.com/questions/17353683/… Commented Jan 8, 2019 at 5:47

4 Answers 4

4

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)

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

5 Comments

True. This won't work. str_contains is Laravel's helper. The question has a Laravel tag
This is better with adding the glue. It still could have issues if values have the glue character in them or if values in $array_2 are substrings of values in $array_1 e.g. if $array_1 = ['A', 'BB', 'C', 'D', 'E', 'F']; it will return true.
The new version won't work if for example $array_1 = ['A', 'C', 'C', 'D', 'E', 'F']; and $array_2 = ['C', 'D', 'E']; :-(
Oh of course. Eh I'm going to sleep. Cool problem, I'm sure there's some really nice solution that we're missing. But not tonight. Keeping up because I think my original solution is really nice if you know that the strings would only be 1 character long.
I believe the solution I posted (using json_encode) solves that robustness issue while retaining the simplicity, but I guess OP doesn't need it :)
3

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

Comments

3

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

Comments

0

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

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.