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

时间:2016-10-12 11:14:57

标签: php arrays multidimensional-array

我试图使用像函数这样的搜索来检索多维数组上的值。

$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'
    )
);

例如,如果你想用一个名为“上帝之城”的名字来搜索数组。和'1963年'预期的输出应该是这样的

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

目前,我正在使用的功能是

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

但是,使用此功能只能检索相同数组键的数据。因此,例如,如果我搜索“上帝之城”的名称值。

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

这将显示正确的输出

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

不幸的是,如果您尝试使用,'名称' => '上帝之城'和'年' => ' 1963' 它不会显示任何输出。我一直坚持要求显示正确的输出,任何想法都会受到赞赏

6 个答案:

答案 0 :(得分:0)

试试这是从How can I check if an array contains a specific value in php?

引用的
$array = array('kitchen', 'bedroom', 'living_room', 'dining_room');

if (in_array('kitchen', $array)) {
    echo 'this array contains kitchen';
}

答案 1 :(得分:0)

试试这个:

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

)

答案 2 :(得分:0)

如果您需要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'
   ),
);

将匹配。

答案 3 :(得分:0)

将搜索功能修改为此功能 - 适用于上述两种情况:

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

答案 4 :(得分:0)

看看我的建议

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'
 *    ]
 *  ];
 */

答案 5 :(得分:-1)

递归电话怎么样?

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

我知道它的摘要,但应该帮助你进行多维搜索