在多维数组上搜索值,然后显示结果



我正在尝试使用搜索类函数检索多维数组上的值。

$rows = array(
    array(
        'Name'=>'City of God',
        'Year'=>'2002',
        'Rating'=>'10'
    ),
    array(
        'Name'=>'The Great Escape',
        'Year'=>'1963',
        'Rating'=>'9'
    ),
    array(
        'Name'=>'Dune',
        'Year'=>'1984',
        'Rating'=>'6'
    ),
    array(
        'Name'=>'Superbabies: Baby Geniuses 2',
        'Year'=>'2004',
        'Rating'=>'1'
    )
);

例如,如果你想搜索数组的Name值为'City of God', Year值为'1963',那么预期的输出应该是这样的

$rows = array(
    array(
        'Name'=>'City of God',
        'Year'=>'2002',
        'Rating'=>'10'
    ),
    array(
        'Name'=>'The Great Escape',
        'Year'=>'1963',
        'Rating'=>'9'
    ),
);

当前,我使用的函数是this

function multiSearch(array $array, array $pairs)
{
    $found = array();
    foreach ($array as $aKey => $aVal) {
        $coincidences = 0;
        foreach ($pairs as $pKey => $pVal) {
            if (array_key_exists($pKey, $aVal) && $aVal[$pKey] == $pVal) {
                $coincidences++;
            }
        }
        if ($coincidences == count($pairs)) {
            $found[$aKey] = $aVal;
        }
    }
    return $found;
} 

但是,使用这个函数只能检索相同数组键的数据。例如,如果我搜索Name of 'City of God'

$x = multiSearch($rows, array('Name' => 'City of God') 

这将显示如下的正确输出

$rows = array(
    array(
        'Name'=>'City of God',
        'Year'=>'2002',
        'Rating'=>'10'
    ),
);

不幸的是,如果你试图使用'Name' => 'City of God'和'Year' => '1963'它不会显示任何输出。我一直在弄清楚如何显示正确的输出,任何想法都会很感激

试试这个:

$datas = array(
    array(
        'Name'=>'City of God',
        'Year'=>'2002',
        'Rating'=>'10'
    ),
    array(
        'Name'=>'The Great Escape',
        'Year'=>'1963',
        'Rating'=>'9'
    ),
    array(
        'Name'=>'Dune',
        'Year'=>'1984',
        'Rating'=>'6'
    ),
    array(
        'Name'=>'Superbabies: Baby Geniuses 2',
        'Year'=>'2004',
        'Rating'=>'1'
    )
);
$search = array(
    'Name' => 'Dune',
    'Year' => '2004'
);
$output = array();
foreach ($datas as $key1 => $row) {
    foreach ($row as $key2 => $value) {
        if($search[$key2] == $value) {
        // if(stristr($value, $search[$key2]) !== FALSE) {  // if you want to search
            $output[] = $datas[$key1];
            break;
        }
    }
}
echo "<pre>"; print_r($output); exit;
输出:

Array
(
    [0] => Array
        (
            [Name] => Dune
            [Year] => 1984
            [Rating] => 6
        )
    [1] => Array
        (
            [Name] => Superbabies: Baby Geniuses 2
            [Year] => 2004
            [Rating] => 1
        )
)

如果您需要OR,正如您在问题中提到的,那么您需要查看是否至少发生了一个巧合:

if ($coincidences > 0) {
   $found[$aKey] = $aVal;
}

这样,两个条目:

$rows = array(
   array(
      'Name'=>'City of God',
      'Year'=>'2002',
      'Rating'=>'10'
   ),
   array(
     'Name'=>'The Great Escape',
     'Year'=>'1963',
     'Rating'=>'9'
   ),
);

将搜索函数修改为-适用于上述两种情况:

function multiSearch($rows, $value) {
  $newArr = array();
  foreach ($rows as $row) {
    foreach ($value as $key => $val) {
      if (isset($row[$key]) && $row[$key] == $val) {
        $newArr[] = $row;
      }
    }
  }
  return $newArr;
}

使用一个或两个值调用产生所需的输出-

$x = multiSearch($rows, array('Name' => 'City of God')); 
$x = multiSearch($rows, array('Name' => 'City of God', 'Year' => '1963'));
//returns $rows containing this pair

看看我的建议

function search ($storage, $criteria)
{
    return array_filter(
       $storage,
       function ($row) use ($criteria) {
           foreach ($criteria as $key => $value) {
               if ($row[$key] === $value) {
                   return true;
               }
           } 
       }
    );
}
$result = search($datas, $search);
/**
 * $result = [
 *    2 => [
 *       'Name'   => 'Dune'
 *       'Year'   => '1984'
 *       'Rating' => '6'
 *    ],
 *    3 => [
 *       'Name'   => 'Superbabies: Baby Geniuses 2'
 *       'Year'   => '2004'
 *       'Rating' => '1'
 *    ]
 *  ];
 */

递归调用呢?

function recursiveInArray ($needle, $array) {
    $status = false;
    foreach($array as $value) {
        if(is_array($value)) {
            recursiveInArray($needle, $value);
        } else {
            $status = ($needle == $value);
        }
    }
    return $status;
}

我知道它是抽象的,但应该可以帮助你进行多维搜索

相关内容

  • 没有找到相关文章

最新更新