【问题标题】:Search for value in multidimensional array and get parent array in PHP在多维数组中搜索值并在PHP中获取父数组
【发布时间】:2012-12-08 11:26:48
【问题描述】:

我有这个数组:

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

【问题讨论】:

  • SO 中有数百个相关问题。
  • 您能否修改您的问题以向我们展示您想要的最终数组,例如“闪亮”?
  • 我的意思是最终数组的假定结构!

标签: php arrays search multidimensional-array


【解决方案1】:

此解决方案将取决于两个共轭循环。

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

【讨论】:

  • 如果您确定 pathMovie 值不能在子数组(二级数组)中重复,您可以在内循环中使用 break 来提高性能。
【解决方案2】:

尚未对此进行测试,但这应该可以:

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 个匹配时才会出现问题。就像我搜索“Texas Chainsaw 3D”一样,我只会得到一个名字。但我想要两个。
【解决方案3】:

我会使用array_filter。有点像

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

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

【讨论】:

    猜你喜欢
    • 2019-09-20
    • 1970-01-01
    • 2012-03-05
    • 2016-12-12
    • 2015-06-15
    • 2011-05-20
    • 2017-07-23
    • 1970-01-01
    • 2012-05-10
    相关资源
    最近更新 更多