LearningThe program in the following example will display the string "you cannot find the value in the array" ("Not found in this array") on the page, because the string Albert you are looking for is indeed not in the $ namesArray array:
The following describes how to use the PHP function in_array ().
- <? $NamesArray=Array("Heart", "Love", "Boy", "Mary", "Paul", "Merry", "Jacky ");
-
- $LookingFor="Albert";
-
- If (in_array ($ lookingFor, $ namesArray )){
-
- Echo "found! ";
-
- } Else {
-
- Echo "you cannot find the value in the array! ";
-
- }
-
- ?>
If you change the value of $ lookingFor to "Mary" and execute the command again, "found" will be displayed on this screen !」 ("You 've got it! "), Because the" Mary "value does exist in the $ namesArray array. If you want to know the total number of elements contained in an array, you can use the easy-to-use count () function:
The value of the variable $ count is 7.
The above is the specific application method of the PHP function in_array.