user3436467
user3436467

Reputation: 1775

validating name and value in array

I have an array that looks like this:

$team = array("tom"=>"35",
"beck"=>"18",
"Gerald"=>"15");

I also am parsing data from a loop which gives me the following strings $name and $num

I would like to build a php if statement that will do the following:

  1. Check if the $name and $num exists in the array $team
  2. Check if the $num matches the assigned number for the name in the array

As an example, lets assume the following:

I have this so far:

if (in_array($num, $team)) {
// do something
}

What's missing is for it to check the second condition mentioned above.

Following the above example, it should fail, since the $num value is "25", whereas it is "35" in the array.

Upvotes: 1

Views: 32

Answers (1)

Sougata Bose
Sougata Bose

Reputation: 31749

You can try to check the $name key with value of $num -

$team = array("tom"=>"35",
"beck"=>"18",
"Gerald"=>"15");

if(isset($team[$name]) && $team[$name] == $num) {
     // Your code
}

in_array will check for the value's existence in the array without considering the key.

Upvotes: 2

Related Questions