Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,995 questions

51,940 answers

573 users

How to check if an array contains a value in PHP

2 Answers

0 votes
$arr = [673, 8, 981, 23, 4];

echo in_array(981, $arr) . "\n"; 

if (in_array(5, $arr))
    echo "yes";
else
    echo "no";
    
    
    

/*
run:

1
no

*/

 



answered Jan 18, 2023 by avibootz
0 votes
// in_array(mixed $needle, array $haystack, bool $strict = false): bool

$arr = [673, 8, 981, "23", 4];
 
echo in_array(23, $arr) . "\n"; 

echo in_array("23", $arr) . "\n"; 

if (in_array(23, $arr, true))
    echo "yes" . "\n"; 
else
    echo "no" . "\n"; 
    
if (in_array("23", $arr, true))
    echo "yes" . "\n"; 
else
    echo "no" . "\n";    
     
     
     
 
/*
run:
 
1
1
no
yes
 
*/

 



answered Jan 18, 2023 by avibootz
...