1

I have a problem that I'm sure is easy to solve with PHP's inbuilt functions or with one or two lines of code, but I cant seem to find a solution using my limited experince.

I have a master array:

$master_array = ('location_1','location_2','location_3','location_4','location_5');

I get given an array:

$submitted_array = ('location_1','location_3');

And I need to compare both arrays to form an array such as this:

$locations = (0,2);

Where the numbers in the $locations array are the locations of the $submitted_array elements in the $master_array.

There must be a way of doing this without loops, surely.

2 Answers 2

3
$result = array_keys(array_intersect($master_array , $submitted_array));
print_r($result);
Sign up to request clarification or add additional context in comments.

Comments

1
$locations = array_keys(array_intersect($master_array, $submitted_array));

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.