需要帮助计算PHP数组的特定数字吗
我是新的需要帮助计算PHP数组的特定数字吗,php,Php,我是新的PHP编程人员,一般来说,我对如何计算特定数字在数组中出现的次数非常迷茫 例如,我需要计算数组中出现数字2的次数 然后我需要将该值转移到另一个变量,以便能够输出它 我尝试了数组\u计数\u值,但似乎无法将其输出 这是我的数组,我需要计算每个数字出现的次数 我试过if语句,我试过所有我读过的东西,都没用,我得到的只是一个空白屏幕 for($counter=0; $counter <=999; $counter++) { $die=rand(2,12); $int[$counter]=
PHP
编程人员,一般来说,我对如何计算特定数字在数组中出现的次数非常迷茫
例如,我需要计算数组中出现数字2
的次数
然后我需要将该值转移到另一个变量,以便能够输出它
我尝试了数组\u计数\u值
,但似乎无法将其输出
这是我的数组,我需要计算每个数字出现的次数
我试过if语句,我试过所有我读过的东西,都没用,我得到的只是一个空白屏幕
for($counter=0; $counter <=999; $counter++)
{
$die=rand(2,12);
$int[$counter]=$die;
echo "$int[$counter],";
}
对于($counter=0;$counter您可以使用如下计数器:
$total = 0;
for($counter=0; $counter <=999; $counter++)
{
$die=rand(2,12);
if($die == 2){
$total ++; //if its two add 1 to total.
}
$int[$counter]=$die;
}
echo 'Total times 2 appeared '. $total;
$total=0;
对于($counter=0;$counter请尝试以下操作:
$rolls = array();
for($x=2;$x<12;$x++)
{
$rolls[$x]=0;
}
for($counter=0; $counter <=999; $counter++)
{
$rolls[rand(2,12)]++;
}
这将输出数字2的滚动次数。if($array[$index]==2)$counter++;
$rolls = array();
for($x=2;$x<12;$x++)
{
$rolls[$x]=0;
}
for($counter=0; $counter <=999; $counter++)
{
$rolls[rand(2,12)]++;
}
echo $rolls[2];