php - Loop though mutiple arrays -
i "googled" , searched on here quite while , have not found reply this. have array of arrays , wish set info in specific order.
here html form. user(s) can add together new rows input more info 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 $_posted values $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 result wanted manually getting values of $data array, want loop though data.
//manual retreaval echo "</br></br>manually getting info $data array</br>"; echo $data['tag'][0] . " - " . $data['model'][0] . " - " . $data['oldroom'][0];
outputs:
tag1 - model1 - oldroom1
so there , how can write php script loop though $data array in format seen above?
tag1 - model1 - oldroom1 - .... tag2 - model2 - oldroom2 - ...
you can utilize foreach()
loop go through values, ie tags:
foreach($data['tag'] $tag) { echo $tag; }
to access same index in different array ie create "match" between old rooms , old owners, use:
foreach($data['oldroom'] $index => $oldroom) { if(isset($data['oldowner'][$index])) echo $oldroom . " belonged " . $data['oldowner'][$index]; else echo $oldroom . " didn't have old owner! :("; }
php arrays multidimensional-array foreach
No comments:
Post a Comment