PHP
·
发表于 5年以前
·
阅读量:8305
php ËÑË÷Êý×e×Ö*u´®ÎÒÃÇÒ»°a»aÓõ½array_searchºÍin_arrayÁ½¸oº¯Êý
array_search()
º¯ÊýÓe in_array()
Ò»Ñu£¬ÔÚÊý×eÖвeÕÒÒ»¸o¼uÖµ¡£Èç¹uÕÒµ½Á˸ÃÖµ£¬Æ¥ÅaÔªËصļuÃu»a±»µ»Ø¡£Èç¹uûÕÒµ½£¬Ôoµ»Ø false¡£
µ«ÊÇarray_searchÒ»°aÓõ½ËÑË÷Ò»¸oÊý×eÖÐuºÏÒªÇoµÄµÚÒ»¸o×Öu´®¡£Èç¹uËÑË÷µÄ×Öu´®ÔÚÊý×eÖк¬Óжa¸o£¬Ê¹ÓÃarray_searchµÄ»°£¬ÊÇ ²»Ðеġ£Õaʱ£¬ÎÒÃÇʹÓÃarray_keys()º¯Êý£¬ÊµÏÖËÑË÷µÄ×Öu´®ÔÚÊý×eÖк¬Óжa¸o£¬
¿´Ò»ÏÂÈçÏÂÀý×Ó£º
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$a=array_search( "blue",$array); //½«Êa³o$a=0;
$b=array_search( 'red',$array); //½«Ö»»aÊa³o$b=1;
$p = array_keys($array, 'red');//ËÑË÷µÄ×Ö*u´®ÔÚÊý×eÖк¬Óжa¸o
if(is_array($p)) {
foreach($p as $v) {
echo $val."³oÏÖÔÚ".$v . " ";
}
}else {
echo $val."³oÏÖÔÚ".array_search($val, $array)." ";
}
Àý¶þ£º
$array = array(4,5,7,8,9,10);
$found = array_search(8, $array); //µ÷ÓÃarray_searchº¯Êý²¢Êa³o²eÕÒ½a¹u
if($found){ //Èç¹uÕÒµ½Êa³o¼u
echo "ÒÑÕÒµ½£¬¼uΪ".$found;
}else{ //Èç¹uûÓÐÕÒµ½Êa³o´iÎoÐÅÏ¢
echo "ûÓÐÕÒµ½";
}
ÏÂÃaeÎÒÃÇÀ´¿´¿´in_arrayº¯Êý
²ÉÓÃin_array(value,array,type)
type ¿ÉÑ¡¡£Èç¹uÉeÖøòÎÊýΪ true£¬Ôo¼i²eËÑË÷µÄÊý¾ÝÓeÊý×eµÄÖµµÄÀaÐÍÊÇ*ñÏaͬ¡£
$arr = array('¿ÉÒÔ','ÈçºÎ','*½*¨','ÖªµÀ','›]ÓÐ','²»Òª');
//in_array(value,array,type)
$isin = in_array("ÈçºÎ2",$arr);
if($isin){
echo "in====".$isin;
}else{
echo "out====".$isin;
}
ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝÁË£¬Ï£Íu´o¼ÒÄܹ»Ï²»¶