开发者

Comparing arrays to produce a key array in PHP

开发者 https://www.devze.com 2023-03-13 22:55 出处:网络
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 limi开发者_StackOverflow中文版ted experince

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 limi开发者_StackOverflow中文版ted 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.


$result = array_keys(array_intersect($master_array , $submitted_array));
print_r($result);


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

精彩评论

暂无评论...
验证码 换一张
取 消