0

I have the following array structure with master data with thousands of records.

Array
   (
        [0] => 0,1
        [1] => 0,0
        [2] => 0,2
        [3] => 0,3
        [4] => 10,2
    )

I have a second array with a smaller subset. Such as

Array
   (
        [0] => 0,1
        [1] => 0,0
    )

I would like to find second array in first array in the extact same order of elements present in second array. But rather than doing an intersect I would like to find the key (or keys) from first array as well. I have been wrecking my brain on this...

UPDATED:

The keys are unique . So far example in above array I would like to see output of:

Array2 found in array1 (starting at key 0).

Second Example:

  Array
   (
        [a] => 0,1
        [b] => 0,0
        [c] => 0,2
        [d] => 0,3
        [e] => 10,2
    )

second array

Array
   (
         [1] => 0,3
         [2] => 10,2

    )

expected output:

second array match in array A, starting at key d of array A..

hope that clears it.

12
  • 1
    I'm assuming these arrays contain strings, e.g. "0,1" and that you've used print_r() to show it here? Are the array values unique? If so, you can array_flip the first array and loop for each value in b (O(n)) and look it up in a (O(1)). Also, perhaps array_intersect_assoc and array_intersect might help you. Commented Jun 14, 2018 at 17:23
  • Show example of desired output given your two arrays. And if you can, answer the question - that 0,1 is actually a string, "0,1".... Commented Jun 14, 2018 at 18:07
  • I've updated the question to reflect the exact ask. Commented Jun 14, 2018 at 18:47
  • In that case it's just an array_intersect. 3v4l.org/gIb10 Commented Jun 14, 2018 at 18:52
  • @andreas array_intersect also matches other elemets from first array that are not in order. For example if I set value of key b to "10,2" then output is "3 elements matched"... That is not what I want. I want to match extactly same amount of element as array B and in same order. Commented Jun 14, 2018 at 23:42

3 Answers 3

0

The best solution to get matching keys is

$result_array = array_intersect_assoc($array1, $array2);
$result_array_keys = array_keys(array_intersect_assoc($array1, $array2));
print_r($result_array);
print_r($result_array_keys); // this gives matching keys array
Sign up to request clarification or add additional context in comments.

Comments

0

I don't understand what is wrong with array_intersect.
As you describe your expected output it seems as array_intersect is perfect.

$a = Array
(
    15 => "0,1",
    16 => "0,0",
    2 => "0,2",
    3 => "0,3",
    4 => "10,2"
);

$b = Array
(
    0 => "0,1",
    1 => "0,0"
);

Var_dump(array_intersect($a, $b));

Output:

array(2) {
  [15]=>  string(3) "0,1"
  [16]=>  string(3) "0,0"
}

https://3v4l.org/KG1v6

Or if MonkeyZeus is correct maybe this can work for you?
I match array_intersect, then make sure the keys is the same.

$intersect = array_intersect($a, $b);
$keys = array_keys($intersect);

If($keys == array_keys($b)){
    Echo "they match";
}else{
    Echo "don't match";
}

https://3v4l.org/iiNmd

After OPs edit it seems it is a simple array_intersect that is needed.

https://3v4l.org/gIb10

$intersect =  array_intersect($a, $b);
Var_dump($intersect);
Echo "matching keys is: " . Implode(", ", array_keys($intersect));

6 Comments

I believe the issue is that OP wants to check if the sequence of items in array2 exist in array1. So from your $a, pretend that key #16 was actually after key #4 so the comparison should fail in that situation.
So you mean it has to be both values and keys that match?
OP mentions exact same order of elements so I assume that keys are irrelevant in the comparison but the returned result should be $a with keys intact so something like array_sequence_intersect() would be an appropriate fictional function name.
Never heard of array sequence intersect. Can't even find anything on Google about it.
You're right, it does not exist. I said that it would be an appropriate "fictional function name". So if you were to make a custom function then array_sequence_intersect() would be a good name for it.
|
0

None of the solutions posted above work. What helped me is to re-structure my array and answer from here: Find array in array, in sequence

6 Comments

That is because you did not explain what you needed or answered the questions asked. That is why I wrote the comment I give up.
@andreas every easy to say that if you can't answer the question. You gave up because you couldn't do it. Simple.
I could tell you to go somewhere but choose not to. But you are wrong, simple as that
@Andreas this is not your personal therapy forum. This is for asking questions and getting answers. If you need someone's shoulder to cry on, this is not the place. You couldn't answer a question that is not the end of the world. You need to chill and stop whining like a little kid. Of you go now.
I just want to make sure anyone who sees this understand that it's not a matter of you are the only one who could solve it. You did not answer the questions we asked you and that meant everyone just left you. If you answer the questions people ask, you generally get at least one correct answer. But by not answering any of the questions below the question people gave up. That is the reality.
|

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.