douping4436
douping4436
2014-10-27 19:03

循环遍历多个数组

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 dou44481 7年前

    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

    点赞 评论 复制链接分享
  • dongyun51582 dongyun51582 7年前

    Use a foreach() loop -

    foreach($data as $key => $value) {
        echo 'key: '.$key.' value: '.$value.'<br>'; 
    }
    
    点赞 评论 复制链接分享
  • dqouryz3595 dqouryz3595 7年前

    You can use a foreach() loop to go through all the values, ie for the tags:

    foreach($data['tag'] as $tag)
    {
        echo $tag;
    }
    

    to access the same index in a different array ie to create a "match" between old rooms and old owners, you could use:

    foreach($data['oldRoom'] as $index => $oldRoom)
    {
         if(isset($data['oldOwner'][$index]))
             echo $oldRoom . " belonged to " . $data['oldOwner'][$index];
         else
             echo $oldRoom . " didn't have an old owner! :(";
    }
    
    点赞 评论 复制链接分享

为你推荐