<?php
$array = [
[
'name' => 'Dick Jansen',
'matchedMovie' => [
[
'nameMovie' => 'Saw',
'genre' => 'Horror',
'patheMovie' => 'Texas Chainsaw 3D',
'patheMovieGenre' => 'Horror',
'score' => '100.00',
]
]
],
[
'name' => 'Jim Scott',
'matchedMovie' => [
[
'nameMovie' => 'Shooter',
'genre' => 'Action, Thriller',
'patheMovie' => 'The Shining',
'patheMovieGenre' => 'Horror, Suspense/Thriller',
'score' => '52.38',
],
[
'nameMovie' => 'Resident Evil Movie',
'genre' => 'Action/Horror',
'patheMovie' => 'Texas Chainsaw 3D',
'patheMovieGenre' => 'Horror',
'score' => '63.16',
]
]
]
];
function findMovieMatches(array $movies, string $movieName): array
{
$result = [];
foreach ($movies as ['name' => $name, 'matchedMovie' => $matches]) {
foreach ($matches as $entry) {
if ($movieName === $entry['patheMovie']) {
$result[] = ['name' => $name] + $entry;
}
}
}
return $result;
}
var_export(findMovieMatches($array, 'Texas Chainsaw 3D'));
echo "\n---\n";
var_export(findMovieMatches($array, 'The Shining'));
preferences:
27.72 ms | 403 KiB | 5 Q