I have two arrays, the second is multidimensional. I'm trying to return a third array where the host_id in Array2 match the values in Array1.
Array1
(
[0] => 146
[1] => 173
)
Array2
(
'localhost' => (
'0' => (
'host_id' => 146
),
),
'192.168.0.43' => (
'1' => (
'host_id' => 160
),
),
'192.168.0.38' => (
'2' => (
'host_id' => 173
)
)
)
So Array3 should be:
Array3
(
[localhost] => Array
'0' => (
'host_id' => 146
),
[192.168.0.38] => Array
'0' => (
'host_id' => 173
),
)
I have tried this, but it's only returning the last matched host_id.
foreach ($Array1 as $value) {
$filtered_hosts = array_filter($Array2, function ($host) use ($value) {
return in_array($host['host_id'], $host_id);
});
}
What am I missing?
$filtered_hostsin foreach... you want to push to $filtered_hosts insteadarray_filter, in_array. Try it out.