Home  >  Article  >  Backend Development  >  PHP array function in_array() finds whether an array value exists

PHP array function in_array() finds whether an array value exists

WBOY
WBOYOriginal
2016-07-25 09:12:47923browse

In php programming, in_array() function searches for a given value in an array.

in_array() definition and usage The in_array() function searches an array for a given value.

Grammar in_array(value,array,type) Parameter Description value required. Specifies the value to search for in the array. array required. Specifies the array to search. type is optional. If this parameter is set to true, it is checked whether the type of the searched data and the value of the array are the same.

Instructions Returns true if the given value value exists in the array array. If the third parameter is set to true, the function returns true only if the element exists in the array and has the same data type as the given value.

If the parameter is not found in the array, the function returns false.

Note: If the value parameter is a string and the type parameter is set to true, the search is case-sensitive.

Example 1, in_array instance.

  1. $people = array("Peter", "Joe", "Glenn", "Cleveland");

  2. if ( in_array("Glenn",$people))

  3. {
  4. echo "Match found";
  5. }
  6. else
  7. {
  8. echo "Match not found";
  9. }
  10. ?>

Copy code

Output: Match found

Example 2, in_array instance.

  1. $people = array("Peter", "Joe", "Glenn", "Cleveland", 23);

  2. if (in_array("23",$people, TRUE))

  3. {
  4. echo "Match found
    ";
  5. }
  6. else
  7. {
  8. echo "Match not found
    ";
  9. }if (in_array("Glenn",$people, TRUE))
  10. {
  11. echo "Match found
    ";
  12. }
  13. else
  14. {
  15. echo "Match not found
    ";
  16. }if ( in_array(23,$people, TRUE))
  17. {
  18. echo "Match found
    ";
  19. }
  20. else
  21. {
  22. echo "Match not found
    ";
  23. }
  24. ?>< ;/p>
Copy the code

output: Match not found Match found Match found



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn