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 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制