0

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="Equipment Tag" name="tag[]"/></td>
        <td><input type="text" value="" placeholder="Equipment 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 retrieval
echo "</br></br>Manually getting data from the $data array</br>";
echo $data['tag'][0] . " - " . $data['model'][0] . " - " . $data['oldRoom'][0];

Output:

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 - ...
2
  • Will each one always hold the same number of entries? Like if there are 10 tags there will be 10 models and 10 old rooms and so on? Commented Oct 27, 2014 at 19:08
  • Yes. They will be set to required. Commented Oct 29, 2014 at 22:18

3 Answers 3

1

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! :(";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ok I did see this searching but it was only outputting the first character. So how can I echo out the first value of each post array that was placed into the $data array? Example: tag1 - model1 - oldRoom1 - oldOwner1 - oldDept1 /newline tag2 - model2 - oldRoom2 - ... ect.
That means your $_POSTed values do not come through as an array but as a string. Either edit the question, including the html form or try a print_r($data); to better visualize what values the array holds.
Just noticed, you need to change in your array declaration the indexes as a string: 'tag' => $_POST['tag'] and if needed then try a print_r($_POST); since the values should be coming through as expected
0

Use a foreach() loop -

foreach($data as $key => $value) {
    echo 'key: '.$key.' value: '.$value.'<br>'; 
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.