Php array function in_array () to find whether the array value exists

Source: Internet
Author: User
Php array function in_array () to find whether the array value exists

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

In_array () defines and uses the in_array () function to search for a given value in the array.

The description value of the in_array (value, array, type) parameter is required. Specifies the value to be searched in the array. Array is required. Specifies the array to be searched. Type is optional. If this parameter is set to true, check whether the data to be searched is of the same type as the value of the array.

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

If no parameter is 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 area 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. ?>

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. ?>

Output: Match not found Match found

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.