Looping through 2 arrays and retrieve the values from the first array if the id matches with the second array. (PHP)

I've been trying to figure out how I would be able to loop through two arrays and match values from each array which should only return the value that matches of the first array.
Array 1:
[0] => Array
(
[contact] => 68
[field] => 11
[value] => DBSA
[cdate] => 2019-11-14T11:21:08-06:00
[udate] => 2021-03-30T07:54:00-05:00
)
[1] => Array
(
[contact] => 68
[field] => 131
[value] => ABC
[cdate] => 2019-11-22T08:34:03-06:00
[udate] => 2021-03-30T07:54:00-05:00
)
Array 2:
[0] => Array
(
[source] => analysis_utm_source
[id] => 131
)
[1] => Array
(
[source] => analysis_utm_medium
[destination] => UTM medium
[id] => 132
)
So in this example I would like to retrieve the values from the first array but only where id/field = 131
I've tried to work with 'array_intersect' but this doesn't seem to give the right output.
I'd appreciate it if someone would be able to push me in the right direction.
EDIT: I've been able to solve it by using array_filter, final code like so;
foreach ($fieldValuesDB as $arr) {
$options[] = $arr['id'];
}
$result = array_filter($fieldValuesAC, function($v) use ($options) {
return in_array($v['field'], $options);
});
Answer
Solution:
foreach($array2 as $k => $v)
{
$key = array_search($v['id'], array_column($array1, 'field'));
if($key){
echo print_r($array1[$key]);
}
}
you will get the index of the element in your first array stored in $key variable
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function format() on string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.