foreach()跳过数组中的第一个元素

时间:2013-01-17 03:46:38

标签: php html

尝试创建GPA计算器。我有一个提交到php文件的表单,然后将表单中的所有数据存储在php数组()中。计算器工作正常,直到我输入相同的值。我认为在我展示一些照片之前这没有意义:

以下是图片中的问题:

Error Scenario

Correct Scenario

因此,在第一张图像中,我输入A,然后输入另一张A,输出数组(1){[0] => float(4.5)}使用var_dump()

在第二个图像中,var_dump()是数组(2){[0] => float(4)1 => float(3.5)}

它正在跳过第一张图片中的第一行...以防万一A在REG中等于4.0而在HONORS中等于4.5。它可能是array_combine()

这是我的php代码:

//$_POST['grades'] for the grades <option> and $_POST['types'] for the type (REG, HONORS)
foreach(array_combine($_POST['grades'], $_POST['types']) as $code => $count)
{

    if ($code == "A")
    {
        if ($count == "REGULAR")
        {
            $GradeArray[] = 4.0;
        }
        else if ($count == "HONORS")
        {
            $GradeArray[] = 4.5;
        }
        else if ($count == "COLLEGE")
        {
            $GradeArray[] = 5.0;
        }
    }
    else if ($code == "B")
    {
        if ($count == "REGULAR")
        {
            $GradeArray[] = 3.0;
        }
        else if ($count == "HONORS")
        {
            $GradeArray[] = 3.5;
        }
        else if ($count == "COLLEGE")
        {
            $GradeArray[] = 4.0;
        }
    }
    else if ($code == "C")
    {
        if ($count == "REGULAR")
        {
            $GradeArray[] = 2.0;
        }
        else if ($count == "HONORS")
        {
            $GradeArray[] = 2.5;
        }
        else if ($count == "COLLEGE")
        {
            $GradeArray[] = 3.0;
        }
    }
    else if ($code == "D")
    {
        if ($count == "REGULAR")
        {
            $GradeArray[] = 1.0;
        }
        else if ($count == "HONORS")
        {
            $GradeArray[] = 1.5;
        }
        else if ($count == "COLLEGE")
        {
            $GradeArray[] = 2.0;
        }
    }
    else if ($code == "F")
    {
        if ($count == "REGULAR")
        {
            $GradeArray[] = 0.0;
        }
        else if ($count == "HONORS")
        {
            $GradeArray[] = .5;
        }
        else if ($count == "COLLEGE")
        {
            $GradeArray[] = 1.0;
        }
    }


}

这可能是整个foreach()语句需要重新编写...如果有人这么说,我就会再次编写逻辑......

我不想用代码阻塞问题所以如果你绝对需要html只是问我将添加一个编辑。

编辑:我也在想我需要重写逻辑......我以前从未使用过array_combine()......我只需要确保与相关的

对应

感谢您的帮助!

4 个答案:

答案 0 :(得分:3)

如果要迭代两个数组(或更多),可以考虑使用MultipleIterator;它不像array_combine()那样破坏数组键。

您还可以通过使用数组来定义每个成绩/类型组合的分数来简化您的逻辑:

$gradesToScores = array(
  'REGULAR' => array(
    'A' => 4.0, 'B' => 3.0, 'C' => 2.0, 'D' => 1.0, 'F' => 0.0,
  ),
  'HONORS' => array(
    'A' => 4.5, 'B' => 3.5, 'C' => 2.5, 'D' => 1.5, 'F' => 0.5,
  ),
  'COLLEGE' => array(
    'A' => 5.0, 'B' => 4.0, 'C' => 3.0, 'D' => 2.0, 'F' => 1.0,
  ),
);

$gradeItemIterator = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$gradeItemIterator->attachIterator(new ArrayIterator($_POST['grades']), 'grade');
$gradeItemIterator->attachIterator(new ArrayIterator($_POST['types']), 'type');

$gradeScores = array();

foreach ($gradeItemIterator as $gradeItem) {
  $gradeScores[] = $gradesToScores[$gradeItem['type']][$gradeItem['grade']];
}

答案 1 :(得分:1)

查看这个数组并输出看看VALUES“a”,它成为结果数组的关键, 所以如果你想要将整个数组与第一个数组的键组合在一起,那么第一个数组必须保留唯一值。

  <?php
    print_r(array_combine(Array('a','a','b'), Array(1,2,3)));
    ?>
    Returns:
    Array
    (
        [a] => 2
        [b] => 3
    )

解决方案(可能不是最好的)

foreach($_POST['grades'] as $KEY=>$code)
{
   $count  = $_POST['types'][$KEY];
   if ($code == "A")
    {
        if ($count == "REGULAR")
        {
            $GradeArray[] = 4.0;
        }
        else if ($count == "HONORS")
        {
            $GradeArray[] = 4.5;
        }
        else if ($count == "COLLEGE")
        {
            $GradeArray[] = 5.0;
        }
    }
    else if ($code == "B")
    {
        if ($count == "REGULAR")
        {
            $GradeArray[] = 3.0;
        }
        else if ($count == "HONORS")
        {
            $GradeArray[] = 3.5;
        }
        else if ($count == "COLLEGE")
        {
            $GradeArray[] = 4.0;
        }
    }
    else if ($code == "C")
    {
        if ($count == "REGULAR")
        {
            $GradeArray[] = 2.0;
        }
        else if ($count == "HONORS")
        {
            $GradeArray[] = 2.5;
        }
        else if ($count == "COLLEGE")
        {
            $GradeArray[] = 3.0;
        }
    }
    else if ($code == "D")
    {
        if ($count == "REGULAR")
        {
            $GradeArray[] = 1.0;
        }
        else if ($count == "HONORS")
        {
            $GradeArray[] = 1.5;
        }
        else if ($count == "COLLEGE")
        {
            $GradeArray[] = 2.0;
        }
    }
    else if ($code == "F")
    {
        if ($count == "REGULAR")
        {
            $GradeArray[] = 0.0;
        }
        else if ($count == "HONORS")
        {
            $GradeArray[] = .5;
        }
        else if ($count == "COLLEGE")
        {
            $GradeArray[] = 1.0;
        }
    }

}

答案 2 :(得分:1)

我同意阿伦的行为原因。可能的解决方案是:

$lookup = Array("A"=>4, "B"=>3, "C"=>2, "D"=>1, "F"=>0);

for ( $i=0; $i<count($_POST['grades']); $i++ ) {
    $temp = $lookup[ $_POST['grades'][$i] ];
    if ( $_POST['types'][$i] == "HONORS" ) {
        $temp += .5;
    }
    elseif ( $_POST['types'][$i] == "COLLEGE" ) {
        $temp += 1;
    }
    $GradeArray[] = $temp;
}

这假定$_POST['grades']$_POST['types']的计数相等 - 否则会导致undefined offset通知或不解决每个值。


关于Arun的代码:嵌套两个循环将创建N * M内循环迭代 - 每个级别/类型组合一个,在这种情况下显然是错误的!我们需要并行遍历数组 ,因为它们用于保存对值。


@Jack:如果我没弄错的话,你需要在数组定义中交换HONORSCOLLEGE

答案 3 :(得分:0)

这个解决方案对我来说很好

$array = array('ab', 'bc', 'cd', 'de');
$array = array_combine(range(1, count($array)), array_values($array));
print_r($array);