PHP 计算变量值出现的次数
PHP count the number of occurances of values of a variable
这与 PHP 中的计数有关,并且有很多问题和示例,但是 none 我可以找到我需要做的事情:计算变量的值和算作自己变量的输出。
我要计数并作为变量输出的值是 50 个州的两个字母缩写中的每一个,即 AL、AK AR...;它们包含在 $state
.
中
计算每个状态出现的次数并将每个状态输出为变量的最有效方法是什么,即$AL_total
和$AK_total
和$AR_total
等?
// I have data in the format
// firstname\n lastname \naddress \ncity \nstate \nzip
// This explode strings into array
foreach($values as $val) {
$valuearray = explode("\n", implode($val));
// This assigns variables that are used to echo each field of data
$firstname = $valuearray[0];
$lastname = $valuearray[1];
$address = $valuearray[2];
$city = $valuearray[3];
$state = $valuearray[4];
$zip = $valuearray[5];
// count $state and output as $AL_total , $AK_total , $AR_total , etc.
// And then...
echo $AL_total;
etc....
我建议使用关联数组,其中键是状态,值是计数,例如:
# before your loop:
$stateCount = [];
# in your loop:
if( ! isset( $stateCount[$state] ) ) { # first time $state is seen, init to 1
$stateCount[$state] = 1;
} else {
$stateCount[$state]++;
}
这可能是一种基于您的初始要求的方法:
#inside the loop
$states[$state] = null;
if (!isset(${$state . '_total'})) {
${$state . '_total'} = 1;
} else {
${$state . '_total'} += 1;
}
然后:
#after the loop
foreach ($states as $state => $_null) {
echo "Total ".$state." = ".${$state . '_total'}."\n";
}
如果您想输出您知道的特定状态:
#after the loop
echo "Total AL = ".((isset($AL_total)) ? $AL_total : 0);
你可以看到它正在运行here
如果您更喜欢使用 array_count_values() 函数获取单个数组(如@nice_dev 所评论),请执行以下操作:
#inside the loop
$states_count[] = $state;
然后:
#after the loop
$count_by_state = array_count_values($states_count);
print_r($count_by_state);
echo "Total AL = ".(isset($count_by_state['AL']) ? $count_by_state['AL'] : 0);
将输出如下内容:
Array
(
[AN] => 1
[AK] => 5
[AR] => 5
[AL] => 4
)
Total AL = 4
可以看到运行here
这与 PHP 中的计数有关,并且有很多问题和示例,但是 none 我可以找到我需要做的事情:计算变量的值和算作自己变量的输出。
我要计数并作为变量输出的值是 50 个州的两个字母缩写中的每一个,即 AL、AK AR...;它们包含在 $state
.
计算每个状态出现的次数并将每个状态输出为变量的最有效方法是什么,即$AL_total
和$AK_total
和$AR_total
等?
// I have data in the format
// firstname\n lastname \naddress \ncity \nstate \nzip
// This explode strings into array
foreach($values as $val) {
$valuearray = explode("\n", implode($val));
// This assigns variables that are used to echo each field of data
$firstname = $valuearray[0];
$lastname = $valuearray[1];
$address = $valuearray[2];
$city = $valuearray[3];
$state = $valuearray[4];
$zip = $valuearray[5];
// count $state and output as $AL_total , $AK_total , $AR_total , etc.
// And then...
echo $AL_total;
etc....
我建议使用关联数组,其中键是状态,值是计数,例如:
# before your loop:
$stateCount = [];
# in your loop:
if( ! isset( $stateCount[$state] ) ) { # first time $state is seen, init to 1
$stateCount[$state] = 1;
} else {
$stateCount[$state]++;
}
这可能是一种基于您的初始要求的方法:
#inside the loop
$states[$state] = null;
if (!isset(${$state . '_total'})) {
${$state . '_total'} = 1;
} else {
${$state . '_total'} += 1;
}
然后:
#after the loop
foreach ($states as $state => $_null) {
echo "Total ".$state." = ".${$state . '_total'}."\n";
}
如果您想输出您知道的特定状态:
#after the loop
echo "Total AL = ".((isset($AL_total)) ? $AL_total : 0);
你可以看到它正在运行here
如果您更喜欢使用 array_count_values() 函数获取单个数组(如@nice_dev 所评论),请执行以下操作:
#inside the loop
$states_count[] = $state;
然后:
#after the loop
$count_by_state = array_count_values($states_count);
print_r($count_by_state);
echo "Total AL = ".(isset($count_by_state['AL']) ? $count_by_state['AL'] : 0);
将输出如下内容:
Array
(
[AN] => 1
[AK] => 5
[AR] => 5
[AL] => 4
)
Total AL = 4
可以看到运行here