0

I know this is a 0(n) relationship, where we need to check for every single row of array for membership, but what is the fastest way to do the check?

$x = [["id" =>61, "name" => "jill"],["id" =>1, "name" => "john"],];
$y = [["id" =>89, "state" => "drunk"],["id" =>61, "state" => "sleep"]];

$z = array_values_collide($x, $y, "id");

where z should return:

$z = [["id" =>61, "name" => "jill", state => "sleep"];

2 Answers 2

1

If I understand correctly, you want all elements in $x that are also in $y. The result is called the intersection of both arrays. See function array_intersect_assoc. I am sure PHP developers took care of implementing it to be as fast as possible.

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

Comments

0

Use array_intersect_assoc to get the values which are present both arrays. And/or check the other intersect commands.

http://www.php.net/manual/en/function.array-intersect-assoc.php

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.