将值推入php中的数组

时间:2016-03-04 23:12:52

标签: php

您好我正在尝试将值从输入推送到数组中。我是php的初学者,这是我的任务。我想制定一个时间表

星期一=这个小时数 星期二=这个小时数 等等

但是在这个过程中我感到很困惑。

<?php


if (isset($_POST['submit'])){


$hours =array();
$hours[] =$_POST['mod1'];
$hours[] =$_POST['mod2'];
$hours[] =$_POST['mod3'];
$hours[] =$_POST['tues1'];
$hours[] =$_POST['tues2'];
$hours[] =$_POST['tues3'];
$hours[] =$_POST['wed1'];
$hours[] =$_POST['wed2'];
$hours[] =$_POST['wed3'];

$wkday = array("Monday" => 0, "Tuesday" => 0, "Wednesday" => 0);
$i = 0;

foreach ($hours as $value){
    if(i<3){
    $wkday[Monday] = $wkday[Monday] + $value;
    } else if(i<6){
        $wkday[Tuesday] = $wkday[Tuesday] + $value;
    } else if(i<9){
        $wkday[Wednesday] = $wkday[Wednesday] + $value;
    }

    $i++;
    }

foreach($wkday as $key => $value) {
    echo "Day: $key; Hours Worked: $value <br /> \n";
    }
}    

?>

5 个答案:

答案 0 :(得分:0)

更快的方法是执行以下操作:

<?php
if (isset($_POST['submit'])){
    $hours =array();
    $hours['Monday'] += (float)$_POST['mod1'];
    $hours['Monday'] += (float)$_POST['mod2'];
    $hours['Monday'] += (float)$_POST['mod3'];
    $hours['Tuesday'] += (float)$_POST['tues1'];
    $hours['Tuesday'] += (float)$_POST['tues2'];
    $hours['Tuesday'] += (float)$_POST['tues3'];
    $hours['Wednesday'] += (float)$_POST['wed1'];
    $hours['Wednesday'] += (float)$_POST['wed2'];
    $hours['Wednesday'] += (float)$_POST['wed3'];
    foreach($hours as $key => $value) 
        echo "Day: $key; Hours Worked: $value <br /> \n";
}    
?>

只需根据$_POST浮点数进行添加,然后像之前一样回显。

答案 1 :(得分:0)

一种方法是定义一个数组,将日期名称映射到您用于输入名称的缩写。

$wkday

然后你可以用这样的嵌套循环填充你的foreach ($days as $key => $day) { // outer loop iterates over 'mod', 'tues', 'wed' for ($i=1; $i <= 3; $i++) { // inner loop iterates over 1, 2, 3 $wkday[$day] += $_POST["$key$i"]; // $key$i gives 'mod1' etc. } // You can echo as you create the array so you don't have to loop again. echo "Day: $day; Hours Worked: $wkday[day]<br>\n"; } 数组:

List<?> list = file1.getList(s);

答案 2 :(得分:0)

如果您可以修改HTML表单,我建议您以这种方式更改输入名称:

<input name="hours[Monday][]">
<input name="hours[Monday][]">
<input name="hours[Monday][]">
<input name="hours[Tuesday][]">
<input name="hours[Tuesday][]">
<input name="hours[Tuesday][]">
(...)

然后,在您的PHP代码中,您只需编写此代码:

if( isset( $_POST['submit'] ) )
{
    $wkday = array( 'Monday' => 0, 'Tuesday' => 0, ... );

    foreach( $wkday as $key => $val )
    {
        $wkday[$key] = array_sum( $_POST['hours'][$key] );
        echo "Day: $key; Hours Worked: {$wkday[$key]} <br /> \n";
    }
}

同样请注意 - 参考原始代码:

  • 您必须使用$引用变量:因此,$i,而不是i;
  • $wkday[Monday](在您的代码中类似)不正确:Monday被解释为常量。在您的情况下,这将起作用,因为没有Monday常量,但如果存在常量,则会产生不希望的结果。请改用$wkday['Monday']

答案 3 :(得分:0)

这是我的HTML:

<body>
     <h1 class="center">Scheduling</h1>

    <div class="table" style="border: solid;">

    <form class='center' method='POST' action='test2.php'>

    <h1> Employee Schedule</h1>

    Name: <input type="text" name="Nam"  required=" " />

    Department: <input type="text" name="Dept"  required=" " />

    Title: <input type="text" name="title" required=" " />

    <h2> Weekdays </h2>

   Monday: <input type="number" name="hours[Monday][]" value="0" require="<8" />
            <input type="number" name="hours[Monday][]" value="0" require="<8" />
          <input type="number" name="hours[Monday][]" value="0" require="<8" />


    Tuesday: <input type="number" name="hours[Tuesday][]" value="0" require="<8" />
            <input type="number" name="hours[Tuesday][]" value="0" require="<8" />
            <input type="number" name="hours[Tuesday][]" value="0" require="<8" />

    Wednesday: <input type="number" name="hours[Wednesday][]" value="0" require="<8" />
            <input type="number" name="hours[Wednesday][]" value="0" require="<8" />
            <input type="number" name="hours[Wednesday][]" value="0" require="<8" />

    Thursday: <input type="number" name="hours[Thursday][]" value="0" require="<8" />
            <input type="number" name="hours[Thursday][]" value="0" require="<8" />
            <input type="number" name="hours[Thursday][]" value="0" require="<8" />

    Friday: <input type="number" name="hours[Friday][]" value="0" require="<8" />
            <input type="number" name="hours[Friday][]" value="0" require="<8" />
            <input type="number" name="hours[Friday][]" value="0" require="<8" />

    Saturday: <input type="number" name="hours[Saturday][]" value="0" require="<8" />
            <input type="number" name="hours[Saturday][]" value="0" require="<8" />
            <input type="number" name="hours[Saturday][]" value="0" require="<8" />

    Sunday: <input type="number" name="hours[Sunday][]" value="0" require="<8" />
            <input type="number" name="hours[Sunday][]" value="0" require="<8" />
            <input type="number" name="hours[Sunday][]" value="0" require="<8" />




             <input type="submit" name="submit" value="Calculate" />

    </form>

这是我的php:

<?php

$Nam = $_POST['Nam'];
$Dept = $_POST['Dept'];
$Title = $_POST['title'];

if( isset( $_POST['submit'] ) )
{
    $wkday = array( 'Monday' => 0, 'Tuesday' => 0, 'Wednesday' => 0, 'Thursday' => 0, 'Friday' => 0, 'Saturday' => 0, 'Sunday' => 0);
    echo "Employee name: $Nam <br /> \n";
        echo "Department: $Dept <br /> \n";
        echo "Title: $Title <br /> \n";

    foreach( $wkday as $key => $val )
    {
        $wkday[$key] = array_sum( $_POST['hours'][$key] );
        echo "Day: $key; Hours Worked: {$wkday[$key]} <br /> \n";
    }
}
?>

答案 4 :(得分:0)

我不认为我完成了我的调查:

创建包含复选框和其他表单元素的表单以创建数组。 AND array()函数用于创建索引和关联数组。

确保访问索引和关联数组中的各个数组元素