欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP使用in_array函數(shù)檢查數(shù)組中是否存在某個(gè)值

 更新時(shí)間:2015年03月25日 09:02:19   作者:kp878  
這篇文章主要介紹了PHP使用in_array函數(shù)檢查數(shù)組中是否存在某個(gè)值,較為詳細(xì)的分析了in_array函數(shù)的功能、定義及相關(guān)的使用技巧與注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了PHP使用in_array函數(shù)檢查數(shù)組中是否存在某個(gè)值的方法。分享給大家供大家參考。具體分析如下:

PHP使用in_array()函數(shù)檢查數(shù)組中是否存在某個(gè)值,如果存在則返回 TRUE ,否則返回 FALSE了,非常的好用,下面我深入來(lái)為各位介紹in_array() 函數(shù).

最近在用php寫一段代碼時(shí),要用到判斷某值是否在另外一組值中。而in_array 函數(shù)就是用來(lái)檢查數(shù)組中是否存在某個(gè)值 。直接通過(guò)概念理解比較模糊,可以通過(guò)具體例子了解其作用。

語(yǔ)法如下:

bool in_array( mixed needle, array array [, bool strict] )

參數(shù)說(shuō)明:

參數(shù) 說(shuō)明
needle 需要在數(shù)組中搜索的值,如果是字符串,則區(qū)分大小寫
array 需要檢索的數(shù)組
strict 可選,如果設(shè)置為 TRUE ,則還會(huì)對(duì) needle 與 array 中的值類型進(jìn)行檢查
例1:

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
 echo "Got Irix";
}
if (in_array("mac", $os)) {
 echo "Got mac";
}
?>

以上代碼的執(zhí)行結(jié)果是:

Got Irix

第二個(gè)條件失敗,因?yàn)?in_array() 是區(qū)分大小寫的。

例2:

<?php
$europe = array("美國(guó)","英國(guó)","法國(guó)","德國(guó)","意大利","西班牙","丹麥");
if (in_array("美國(guó)",$europe)) {
echo "True";
}
?>

同上面一樣,執(zhí)行結(jié)果為True 。

例3:嚴(yán)格類型檢查例子

<?php
$a = array('1.10', 12.4, 1.13);
if (in_array('12.4', $a, true)) {
 echo "'12.4' found with strict check ";
}
if (in_array(1.13, $a, true)) {
 echo "1.13 found with strict check ";
}
?>

其輸出結(jié)果是:

1.13 found with strict check

例4:數(shù)組中套用數(shù)組

<?php
$a = array(array('p', 'h'), array('p', 'r'), 'o');
if (in_array(array('p', 'h'), $a)) {
 echo "'ph' was found ";
}
if (in_array(array('f', 'i'), $a)) {
 echo "'fi' was found ";
}
if (in_array('o', $a)) {
 echo "'o' was found ";
}
?>

其輸出結(jié)果為:

  'ph' was found
  'o' was found

其具體用法如下:

bool in_array(mixed $needle,array $haystack [, bool $strict = FALSE ])

在 haystack 中搜索 needle,如果沒有設(shè)置 strict 則使用寬松的比較。

注:自php5.4以后。數(shù)組定義由array()換成了array[] 。

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論