I have this array:
Array
(
[0] => Array
(
[name] => Dick Jansen
[matchedMovie] => Array
(
[0] => Array
(
[nameMovie] => Saw
[genre] => Horror
[patheMovie] => Texas Chainsaw 3D
[patheMovieGenre] => Horror
[score] => 100.00
)
)
)
[1] => Array
(
[name] => Jim Scott
[matchedMovie] => Array
(
[0] => Array
(
[nameMovie] => Shooter
[genre] => Action, Thriller
[patheMovie] => The Shining
[patheMovieGenre] => Horror, Suspense/Thriller
[score] => 52.38
)
[1] => Array
(
[nameMovie] => Resident Evil Movie
[genre] => Action/Horror
[patheMovie] => Texas Chainsaw 3D
[patheMovieGenre] => Horror
[score] => 63.16
)
)
)
)
I want to search on a [patheMovie] value (like 'The Shining') and get the parent array with the [name] plus only the [matchedMovie] array with the matched [patheMovie] back.
I tried something like this:
$search='Texas Chainsaw 3D';
$sorted=false;
foreach ($sorted as $n=>$c)
if (in_array($search,$c)) {
$cluster=$n;
break;
}
if i search for 'The Shining' for example i want the array to return like this:
Array
(
[0] => Array
(
[name] => Dick Jansen
[nameMovie] => Saw
[genre] => Horror
[patheMovie] => Texas Chainsaw 3D
[patheMovieGenre] => Horror
[score] => 100.00
)
)
and if you search for 'Texas Chainsaw 3D' like so:
Array
(
[0] => Array
(
[name] => Dick Jansen
[nameMovie] => Saw
[genre] => Horror
[patheMovie] => Texas Chainsaw 3D
[patheMovieGenre] => Horror
[score] => 100.00
)
[1] => Array
(
[name] => Jim Scott
[nameMovie] => Resident Evil Movie
[genre] => Action/Horror
[patheMovie] => Texas Chainsaw 3D
[patheMovieGenre] => Horror
[score] => 63.16
)
)
This solution will depend into two conjugated loops.
<?php
function searchIt($arr, $searchItem){
$result = array();
$resultIndex = 0;
for ($i =0; $i < count($arr); $i++){
for ($j = 0; $j < count($arr[$i]['matchedMovie']); $j++){
if ($arr[$i]['matchedMovie'][$j]['patheMovie'] == $searchItem){
$result[$resultIndex]['name'] = $arr[$i]['name'];
foreach ($arr[$i]['matchedMovie'][$j] as $key => $value){
$result[$resultIndex][$key] = $value;
}
$resultIndex++;
}
}
}
return $result;
}
?>
Haven't tested this, but this should work:
function findYourGuy($array, $searchTerm) {
$searchTerm = 'The Shining'; // testing purpose only
foreach($array as $personArray) {
$matchedMovies = $personArray['matchedMovie'];
$name = $personArray['name'];
foreach($matchedMovies as $matchedMovie) {
if($matchedMovie['patheMovie'] == $searchTerm) {
return array('name' => $name, 'matchedMovie' => $matchedMovie)
}
}
}
return false; //no result
}
I would use array_filter. Something along the lines
$movieName = 'The shining';
$result = array_filter($movies, filterCallback);
function filterCallback($var)
{
foreach($var['matchedMovie'] as $movie) {
if($movie['PatheMovie'] == $movieName) {
return true;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With