douping4436 2014-10-27 19:03
浏览 273
已采纳

循环遍历多个数组

I have an array of arrays and wish to put the data in a specific order.

Here is my html form. The user(s) can add new rows to input more data as necessary.

    <tr>
        <td><input type="text" value="" placeholder="Date of Transfer" name="date[]"/></td>
        <td><input type="text" value="" placeholder="Equpment Tag" name="tag[]"/></td>
        <td><input type="text" value="" placeholder="Equpment Model" name="model[]"/></td>
        <td><input type="text" value="" placeholder="Current Room" name="oldRoom[]"/></td>
        <td><input type="text" value="" placeholder="Current Owner" name="oldOwner[]"/></td>
        <td><input type="text" value="" placeholder="Current Dept" name="oldDept[]"/></td>
        <td><input type="text" value="" placeholder="New Room" name="newRoom[]"/></td>
        <td><input type="text" value="" placeholder="New Owner" name="newOwner[]"/></td>
        <td><input type="text" value="" placeholder="New Dept" name="newDept[]"/></td>
    </tr>
</tbody>

<tfoot>
    <tr>
        <td colspan="3"><a href="javascript:void(0);" id='anc_add'>Add Row</a></td>
        <td colspan="3"><a href="javascript:void(0);" id='anc_rem'>Remove Row</a></td>
        <td colspan="3"><button type="submit">Submit</button></td>
    </tr>

Then I'm putting the $_POSTed values into the $data array variable.

$data = array(  tag => $_POST['tag'],
                model => $_POST['model'], 
                oldRoom => $_POST['oldRoom'],
                oldOwner => $_POST['oldOwner'],
                oldDept => $_POST['oldDept'],
                newRoom => $_POST['newRoom'],
                newOwner => $_POST['newOwner'],
                newDept => $_POST['newDept']
                );

I figured out how to get the result i wanted manually getting the values of the $data array, but want to loop though all the data.

//manual retreaval
echo "</br></br>Manually getting data from the $data array</br>";
echo $data['tag'][0] . " - " . $data['model'][0] . " - " . $data['oldRoom'][0];

outputs:

tag1 - model1 - oldRoom1

So is there and how can I write a php script to loop though the $data array in the format as seen above?

tag1 - model1 - oldRoom1 - .... tag2 - model2 - oldRoom2 - ...

  • 写回答

3条回答 默认 最新

  • dou44481 2014-10-29 14:14
    关注

    To get the output that I was looking for I needed to create a for loop. First I needed to count how many values were stored, then loop though till $i was <= $c - 1.

    $c = count($data['tag']);
    
    for ($i = 0; $i <= $c -1 ; $i++) {
    echo $data['tag'][$i] . " - " . 
         $data['model'][$i] . " - " . 
         $data['oldRoom'][$i] . " - " .
         $data['oldOwner'][$i] . " - " .
         $data['oldDept'][$i] . " - " .
         $data['newRoom'][$i] . " - " .
         $data['newOwner'][$i] . " - " .
         $data['newDept'][$i] . "</br>";
    }
    

    Outputs:

    tag1 - model1 - room1 - owner1 - dept1 - room1 - owner1 - dept1

    tag2 - model2 - room2 - owner2 - dept2 - room2 - owner2 - dept2

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里