0

the following is a multidimensional array; from where i have tried to filter out the list of all sub arrays contents where "level" value is equal to 4 by using code (mentioned below); Can some one direct me how to gain the same list with two or more conditions applied to the same code.

Ex 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] => 
        )

the code use to filter is as mentioned below,

$filter_val1 = 4;

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

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

1 Answer 1

2

Try this:

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

The closure checks each rule, it returns true only when all rules are satisfied for that row.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.