[英]php associative existence check
我有
$a = array('ten','ten','ten','three','two','one','ten','four','four');
$b= array_count_values ($a);
我試圖返回找到$ c時特定字符串$ c在$ a中出現的次數。
因此,例如:
如果$ c ='four'然后我需要輸出2
如果$ c ='fout'然后我需要輸出為'not found'
我是php新手,語法有問題,尤其是因為這是一個關聯數組。
我該怎么做呢?
PHP手冊指出array_key_exists返回一個關聯數組,其中key為值,value為頻率。 因此,您可以檢查變量$a
是否是$b
的鍵,以及它是否回顯了頻率計數。
$a = array('ten','ten','ten','three','two','one','ten','four','four');
$b= array_count_values ($a);
if(array_key_exists($c, $b))
{
echo $b[$c];
}
else
{
echo 'not found';
}
echo $b['four']; // echoes 2, since there's two 'four' values in the array.
對於自定義的“未找到”,您將需要以下內容:
echo isset($b['fout']) ? "it's there" : "it's not there";
$b = array_count_values($a);
$c = "four";
if (isset($b[$c]))
{
echo $b[$c];
}
else echo "not found";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.