php数组函数序列之in_array() 查找数组值是否存在

高洛峰
Libérer: 2023-03-04 13:20:02
original
1648 Les gens l'ont consulté

in_array() 定义和用法 
in_array() 函数在数组中搜索给定的值。 

语法 
in_array(value,array,type) 
参数 描述 
value 必需。规定要在数组搜索的值。 
array 必需。规定要搜索的数组。 
type 可选。如果设置该参数为 true,则检查搜索的数据与数组的值的类型是否相同。 

说明 
如果给定的值 value 存在于数组 array 中则返回 true。如果第三个参数设置为 true,函数只有在元素存在于数组中且数据类型与给定值相同时才返回 true。 

如果没有在数组中找到参数,函数返回 false。 

注释:如果 value 参数是字符串,且 type 参数设置为 true,则搜索区分大小写。 

例子 1 

<?php 
$people = array("Peter", "Joe", "Glenn", "Cleveland"); 

if (in_array("Glenn",$people)) 
{ 
echo "Match found"; 
} 
else 
{ 
echo "Match not found"; 
} 
?>
Copier après la connexion

输出:

Match found
例子 2

<?php 
$people = array("Peter", "Joe", "Glenn", "Cleveland", 23); 

if (in_array("23",$people, TRUE)) 
{ 
echo "Match found<br />"; 
} 
else 
{ 
echo "Match not found<br />"; 
}if (in_array("Glenn",$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 />"; 
} 
?>
Copier après la connexion

输出: 

Match not found 
Match found 
Match found 

更多php数组函数序列之in_array() 查找数组值是否存在相关文章请关注PHP中文网!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!