在多维数组中搜索值并在PHP中获取父数组

时间:2012-12-20 21:25:51

标签: php arrays search multidimensional-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
                        )

                )

        )
)

我想搜索一个[patheMovie]值(比如'The Shining')并获取带有[name]的父数组加上只有匹配[patheMovie]的[matchedMovie]数组。

我试过这样的事情:

$search='Texas Chainsaw 3D';

                $sorted=false;
                foreach ($sorted as $n=>$c)
                    if (in_array($search,$c)) {
                        $cluster=$n;
                    break;
                }

如果我搜索'The Shining',例如我希望数组返回如下:

    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
              )
      )

3 个答案:

答案 0 :(得分:8)

该解决方案将依赖于两个共轭环。

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

答案 1 :(得分:1)

尚未对此进行测试,但这应该有效:

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
}

答案 2 :(得分:1)

我会使用array_filter。一些事情

$movieName = 'The shining';
$result = array_filter($movies, filterCallback);

function filterCallback($var)
{
  foreach($var['matchedMovie'] as $movie) {
    if($movie['PatheMovie'] == $movieName) {
      return true;
    }
  }
}