有没有办法从1开始计算数组的键并计算到数组的结尾(下例中为10)。 因此,如果选中所有复选框,则结果应为55(1 + 2 + 3 ... + 10)。我从1开始分配了密钥,但它没有工作。 这就是我到目前为止所拥有的。 这是数组(值现在无关紧要,稍后会处理):
<?php
$rank1_1[1] = '<input type = "checkbox" name = "v_1[]" value="1" />h';
$rank1_1[2] = '<input type = "checkbox" name = "v_1[]" value="7" />w';
$rank1_1[3] = '<input type = "checkbox" name = "v_1[]" value="20" />n';
$rank1_1[4] = '<input type = "checkbox" name = "v_1[]" value="44" />m';
$rank1_1[5] = '<input type = "checkbox" name = "v_1[]" value="81" />l';
$rank1_1[6] = '<input type = "checkbox" name = "v_1[]" value="133" />k';
$rank1_1[7] = '<input type = "checkbox" name = "v_1[]" value="203" />p';
$rank1_1[8] = '<input type = "checkbox" name = "v_1[]" value="292" />b';
$rank1_1[9] = '<input type = "checkbox" name = "v_1[]" value="403" />s';
$rank1_1[10] = '<input type = "checkbox" name = "v_1[]" value="537" />b';
?>
过程:
<?php
for ($i = 1; $i <= count($rank1_1); $i++)
echo $rank1_1[$i], '<br />';
$keys = array_keys($checked_1);
foreach($keys as $keys_s) {
$keys_sum += $keys_s;
}
?>
结果:
<?php
echo '<br />$keys_sum = '. $keys_sum; // $keys_sum = 45
print_r ($keys_sum); // Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3
[4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 )
?>
此外,如果我检查第8,第9和第10个框,结果是:
Array ( [0] => 0 [1] => 1 [2] => 2 [3]
如何让它按预期工作?请帮忙。
答案 0 :(得分:0)
假设$ rank1_1数组中的每个键值对都被视为&#34;检查&#34;,那么你的答案很简单:
$rank1_1[1] = '<input type = "checkbox" name = "v_1[]" value="1" />h';
$rank1_1[2] = '<input type = "checkbox" name = "v_1[]" value="7" />w';
$rank1_1[3] = '<input type = "checkbox" name = "v_1[]" value="20" />n';
$rank1_1[4] = '<input type = "checkbox" name = "v_1[]" value="44" />m';
$rank1_1[5] = '<input type = "checkbox" name = "v_1[]" value="81" />l';
$rank1_1[6] = '<input type = "checkbox" name = "v_1[]" value="133" />k';
$rank1_1[7] = '<input type = "checkbox" name = "v_1[]" value="203" />p';
$rank1_1[8] = '<input type = "checkbox" name = "v_1[]" value="292" />b';
$rank1_1[9] = '<input type = "checkbox" name = "v_1[]" value="403" />s';
$rank1_1[10] = '<input type = "checkbox" name = "v_1[]" value="537" />b';
echo array_sum( array_keys( $rank1_1 ) ); // 55
echo '<br />';
echo '<br />';
但是如果您需要确保在$ checked_1中检查值,那么我只能假设您的值为1,7,20,44等。
$checked_1 = array( 1, 7, 20, 44 );
在这种情况下,你需要像这样匹配:
$i = 1;
foreach( $rank1_1 as $key => $value )
{
foreach( $checked_1 as $x )
{
if( strpos( $value, 'value="' . $x . '"') !== FALSE )
{
$found[] = $i;
$i++;
break;
}
}
}
if( isset( $found ) )
echo array_sum( $found ); // 10
尽管如此,如果任何$ checked_1元素只是添加到您的计数中,并且总是从1开始,那么您可能只是这样做:
$total = 0;
for( $x = 1; $x <= count( $checked_1 ); $x++ )
{
$total += $x;
}
如果这些都没有帮助你,我想你需要举例说明$ checked_1的样子,以及你的期望是什么。
更新-----------------
关于你最近的评论,你的意思是这样的:
foreach( $rank1_1 as $key => $value )
{
foreach( $checked_1 as $x )
{
if( strpos( $value, 'value="' . $x . '"') !== FALSE )
{
$found[] = $x;
break;
}
}
}
if( isset( $found ) )
echo array_sum( $found );
更新2 --------------------
键的总和:
foreach( $rank1_1 as $key => $value )
{
foreach( $checked_1 as $x )
{
if( strpos( $value, 'value="' . $x . '"') !== FALSE )
{
$found[] = $key;
break;
}
}
}
if( isset( $found ) )
echo array_sum( $found );