Change language

How to determine if an array contains a specific value in PHP?

The following programs show how to determine if an array contains a particular value:Program 1:
// array contains elements $names = array ( ’Geeks ’ , ’ for’ , ’Geeks’ );  
// element search $item = ’Geeks’ ; if (in_array ( $item , $names )) { // there is an item echo "Your element founded" ; } else { // no item echo "Element is not found" ; }
?>
Output:
Your element founded 
Program 2:In this example, the in_arrayfunction works in strict mode, it also checks the type of array elements. $name = array ( " Python " , "Java" , "C # " , 3); // Find python if (in_array ( " python " , $name , TRUE)) { echo " found " ; } else { echo "not found" ; } // Seraching 3 if (in_array (3, $name , TRUE)) { echo "found" ; } else { echo "not found" ; } // Seraching Java if (in_array ( "Java" , $name , TRUE)) { echo "found" ; } else { echo "not found" ; } ?> Output:
not found found found 
Program 3: $arr = array ( ’gfg’ , 1, 17); if (in_array ( ’ gfg’ , $arr )) { echo "gfg" ; } if (in_array (18, $arr )) { echo "18 found with strict check" ; }
?>
Output:
gfg 
In the above example, the array contains the string "gfg" and two numbers 1 and 17. The if statement calls the in_array() functionwhich checks if the given element as a parameter in the given array or not. If the function returns true, the if statement is executed.

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method