Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for value in multidimensional array and get parent array in PHP

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
              )
      )
like image 634
user1386906 Avatar asked Dec 20 '12 21:12

user1386906


3 Answers

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;
}
?>

phpfiddle demo

like image 101
SaidbakR Avatar answered Nov 11 '22 14:11

SaidbakR


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
}
like image 29
Freeman L Avatar answered Nov 11 '22 14:11

Freeman L


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;
    }
  }
}
like image 1
jaudette Avatar answered Nov 11 '22 16:11

jaudette