Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php checking array for number

Tags:

arrays

php

Whats the easiest and quickest way to loop through an array of numbers and find out if $val is in there?

Say my array is $numbers(3,5,78,35,78) and my $val=5, it would return true.

like image 836
user1022585 Avatar asked Apr 21 '26 21:04

user1022585


2 Answers

Maybe in_array()?

if (in_array($val, $numbers)) {
  // Do something
}

No looping required

like image 150
Clive Avatar answered Apr 23 '26 10:04

Clive


If you don't care about the position of your number, you could use in_array()

like image 32
professorsloth Avatar answered Apr 23 '26 11:04

professorsloth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!