1

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

  • The $name string returns a team members name from a database
  • The $num string returns a team members number from a database

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:

  • $name = "tom";
  • $num = "25";

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.

1 Answer 1

2

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.

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.