0

I know i could do this with a set of loops, but just wondering if there is a more efficient way.

I have a 2D array with columns of 'id' and 'name' (it's dynamic).

And i query my database asking it to return all records that match the list of ids. That's all done.

Does anyone know how i could merge the query results with my initial array.

Long story short, i want to print out the only the names of ids that are in my database.

$array1 = [[1,'bob'],[2,'jim']];
$array2 = [[2,'false','true']];

i'd like output of:

$array3 = [[2,'jim','false','true']]

I guess a better description is that i'd like to inner join two arrays.

3
  • Please provide the structures of combining arrays Commented Mar 23, 2014 at 18:59
  • use foreach loop and make a query for each id Commented Mar 23, 2014 at 18:59
  • that would be pretty network intensive i think @iamsleepy Commented Mar 23, 2014 at 20:10

1 Answer 1

0

Just as @Mihai said, you can use array_intersect to archive this.

So basically if you have 2 different arrays,

1. $array1 #results from the database
2. $array2 #results which you already have containing the ids

You can use the array_intersect which will compute the intersection of the arrays.

array array_intersect ($array1 ,$array2 );
Sign up to request clarification or add additional context in comments.

1 Comment

Alas, that would only work if array1 and array2 had similar structures. If they did, i could just use array2.

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.