0

How do I find the values in an array that matches another array in order?

Here is my code that gives me the $Array4 which does not correspond to the expected result (given below):

<?php
for ($j=0; $j < 1; $j++) {
    for ($i=0; $i < 100; $i++) {
        $Array3 = (array_intersect($Array2, $Array1));
        $Array4 = array_unique($Array3);
    }

print_r($Array4);
}
?>

$Array1 :

[not] => G
[have] => L

$Array2 - Array who match with $Array1:

[Once] => B
[uppon] => A
[a] => G
[time] => M
[,] => Z
[a] => V
[small] => G
[squirrel] => F
[,] => Z
[whitch] => U
[once] => L
[in] => N
[the] => N
[forest] => X
[,] => Z
[set] => G      \\Search 
[out] => L      \\string
[to] => V
[find] => M
[something] => N
[to] => W
[eat] => X
[,] => Z
[to] => G
[survive] => G
[.] => Z

The result with my code:

$Array3 - with duplicates:

[a] => G
[small] => G
[once] => L
[set] => G     \\Search 
[out] => L     \\string
[to] => G

$Array4 - The result (the problem being that "a" and "once" do not follow each other in the array $Array2) :

[a] => G
[once] => L

The expected result:

[set] => G    \\Search 
[out] => L    \\string 
Script_Coded
  • 709
  • 8
  • 21
Marie
  • 13
  • 4
  • 2
    This is a very strange situation. And even though you tried I feel like you did not manage to explain what you want. I would suggest you provide the data that's in `Array1` & `Array2` and then the result you get and the result you want to get. This might just be me though. – Mihailo Jan 03 '17 at 19:32
  • @Milailo The totality of the data of Array1 & Array2 are given above. As well the result obtained and the desired result are also given above. – Marie Jan 03 '17 at 19:39
  • what is the purpose of the keys `not` and `have` in `$Array1` ? – CodeGodie Jan 03 '17 at 20:04

1 Answers1

0

This would hopefully do what you are looking for.

function findSameInPosition($needle, $haystack) {
    $indexedNeedle = array_values($needle);
    $indexedHaystack = array_values($haystack);
    foreach (array_keys($indexedHaystack) as $key) {
        if (array_slice($indexedHaystack, $key, count($indexedNeedle)) === $indexedNeedle) {
            return array_slice($haystack, $key, count($indexedNeedle));
        }
    }
}

// Example:

$input1 = array(
    "not" => "G",
    "have" => "L"
);

$input2 = array(
    "Once" => "B",
    "uppon" => "A",
    "a" => "G",
    "time" => "M",
    "," => "Z",
    "a" => "V",
    "small" => "G",
    "squirrel" => "F",
    "," => "Z",
    "whitch" => "U",
    "once" => "L",
    "in" => "N",
    "the" => "N",
    "forest" => "X",
    "," => "Z",
    "set" => "G",
    "out" => "L",
    "to" => "V",
    "find" => "M",
    "something" => "N",
    "to" => "W",
    "eat" => "X",
    "," => "Z",
    "to" => "G",
    "survive" => "G",
    "." => "Z"
);

findSameInPosition($input1, $input2); // Returns Array ( [set] => G [out] => L )
Script_Coded
  • 709
  • 8
  • 21
  • Thank you very much for your answer. However, I do not understand why when I change my values ​​in the array, for example by putting "V" "M" instead of "G" "L" the result is this : [a] => V [time] => M Whereas it should be [to] => V [find] => M ? – Marie Jan 03 '17 at 21:06
  • That is because an array can not have multiple values with the same key. The last one to be defined will overwrite the previously defined. If that doesn't suit your needs you will have to use another data structure for having multiple keys. If that is the case I encourage you to raise a new question with a data structure similar to the one mentioned in the following answer: http://stackoverflow.com/a/2879138/4493079 – Script_Coded Jan 03 '17 at 21:15
  • Thank you :) It is indeed a problem but I can not do otherwise ... It is for this reason that I was looking for a system allowing me to take definite values, fixed between them. (y) – Marie Jan 03 '17 at 22:02
  • Well, then I'm sad to tell you your problem is without a solution. Unless you could create an array with a structure like this: `array( array("Once" => "B"), array("uppon" => "A"), array("a" => "G") )` and so on. – Script_Coded Jan 03 '17 at 22:37