$a = array('ten','ten','ten','three','two','one','ten','four','four');
$b= array_count_values ($a);
我试图返回,如果$ C找到一个特定的字符串$ C出现在$一个的次数。
因此,例如:
如果$ C = '四' 然后,我需要输出2
如果$ C = 'f输出' 然后,我需要为 '未发现'
我输出新的PHP和我有语法的麻烦,特别是因为这是一个关联数组。
我该怎么做?
$a = array('ten','ten','ten','three','two','one','ten','four','four');
$b= array_count_values ($a);
我试图返回,如果$ C找到一个特定的字符串$ C出现在$一个的次数。
因此,例如:
如果$ C = '四' 然后,我需要输出2
如果$ C = 'f输出' 然后,我需要为 '未发现'
我输出新的PHP和我有语法的麻烦,特别是因为这是一个关联数组。
我该怎么做?
PHP手册指出array_key_exists返回一个关联数组,其中键为值,值为频率。因此,您可以检查变量$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";