1.

How To Find A Specific Value In An Array?

Answer»

There are two FUNCTIONS can be USED to TEST if a value is defined in an ARRAY or not:

  • array_search($value, $array) - Returns the first key of the matching value in the array, if found. Otherwise, it returns false.
  • in_array($value, $array) - Returns true if the $value is defined in $array.

Here is a PHP script on how to use arrary_search():

<?php $array = array("Perl", "PHP", "Java", "PHP"); PRINT("Search 1: ".array_search("PHP",$array)."n"); print("Search 2: ".array_search("Perl",$array)."n"); print("Search 3: ".array_search("C#",$array)."n"); print("n"); ?>

This script will print:

Search 1: 1
Search 2: 0
Search 3:

There are two functions can be used to test if a value is defined in an array or not:

Here is a PHP script on how to use arrary_search():

This script will print:

Search 1: 1
Search 2: 0
Search 3:



Discussion

No Comment Found