0

下面是一个多维数组;我试图通过使用代码(如下所述)过滤掉“级别”值等于4的所有子数组内容的列表;有人可以指导我如何通过将两个或多个条件应用于同一代码来获得相同的列表。

前任roleid =3 && level = 4 && queryid=59

//array as follows
$Row_Hedder =
Array
(

    [1] => Array
        (
            [username] => Suman
            [roleid] => 3
            [password] => drf123
            [level] => 3
            [queryid] => 59
            [year] => 2013
            [month] => 1
            [date] => 1
            [pagezone11] => 1
            [visiblename] => Suman
            [em1] => 
            [em2] => 
        )

    [2] => Array
        (
            [username] => Pranav
            [roleid] => 3
            [password] => drf123
            [level] => 3
            [queryid] => 59
            [year] => 2013
            [month] => 1
            [date] => 1
            [pagezone11] => 2
            [visiblename] => Pranav
            [em1] => 
            [em2] => 
        )

用于过滤的代码如下所述,

$filter_val1 = 4;

$filter = function($player) use($filter_val1) { return ($player['level'] == $filter_val1); };

$filtered = array_filter($Row_Hedder, $filter);
4

1 回答 1

1

试试这个:

$filter_rules = array(
  'level' => 4,
  'roleid' => 3,
  'queryid' => 59,
);

$filter = function ($player) use ($filter_rules) {
  foreach ($filter_rules as $rulekey => $rulevalue) {
    if ($player[$rulekey] != $rulevalue) {
      return false; 
    }
  }
  return true; 
};

$filtered = array_filter($Row_Hedder, $filter);

闭包检查每个规则,只有当该行的所有规则都满足时,它才返回 true。

于 2013-10-05T21:42:03.350 回答