تابع in_array() در PHP
مثال
در آرایه به دنبال مقدار "Glenn" بگرد و متنی را چاپ کن:
<?php $people = array("Bill", "Steve", "Mark", "David"); if (in_array("Mark", $people)) { echo "متن یافت شد"; } else { echo "متن یافت نشد"; } ?>
تعریف و استفاده
درخواست in_array() برای جستجوی وجود یک مقدار مشخص در یک آرایه.
Note:if search The parameter is a string and type If the parameter is set to TRUE, the search is case-sensitive.
Syntax
in_array(search,array,type)
Parameter | Description |
---|---|
search | Required. Specifies the value to be searched in the array. |
array | Required. Specifies the array to be searched. |
type | Optional. If this parameter is set to true, it checks whether the search data and the type of the array values are the same. |
Description
If the given value search exists in the array array is returned true. If the third parameter is set to true, the function returns true only if the element exists in the array and the data type is the same as the given value.
If the parameter is not found in the array, the function returns false.
Note:if search The parameter is a string and type If the parameter is set to true, the search is case-sensitive.
Technical Details
Return Value: | If the value is found in the array, it returns TRUE, otherwise it returns FALSE. |
PHP Version: | 4+ |
Changelog: | Since PHP 4.2,search The parameter may also be an array now. |
More Examples
Example 1
Use all parameters:
<?php $people = array("Bill", "Steve", "Mark", "David"); if (in_array("23", $people, TRUE)) { echo "Match found<br>"; } else { echo "Match not found<br>"; } if (in_array("Mark",$people, TRUE)) { echo "Match found<br>"; } else { echo "Match not found<br>"; } if (in_array(23,$people, TRUE)) { echo "Match found<br>"; } else { echo "Match not found<br>"; } ?>