5

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
              )
      )
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
user1386906
  • 1,161
  • 6
  • 26
  • 53

3 Answers3

8

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

potashin
  • 44,205
  • 11
  • 83
  • 107
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
  • If you are sure that the pathMovie value could not be repeated in a sub array (second level array) you are able to use break in the inner loop to increase performance. – SaidbakR Dec 20 '12 at 22:42
1

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
}
Freeman
  • 1,201
  • 1
  • 11
  • 20
  • thank you for your anwser. this helps alot, problem is only when there are 2 matched in the array. Like if i search for 'Texas Chainsaw 3D', i only get one name back. But i want to get two. – user1386906 Dec 20 '12 at 22:02
1

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;
    }
  }
}
jaudette
  • 2,305
  • 1
  • 20
  • 20