cedricpatrick Posted January 2, 2021 Share Posted January 2, 2021 Hello, I am a PHP biginner. I would be grateful if you could help with the issue below. I have created three input dynamic variables in a separate php file using a For lopp. <?php For ($i=1; $i<=31; $i++) { echo '<div class="row">'; echo '<div class="col span-1-of-4">'; //Dates en français $jour = $jour + 1; $dateTest = $annee . "-" . $mois . "-" . $jour; $date = date_create("$annee-$mois-$jour"); echo '<input name="workday[]" id="workday[]" class="donnee-pointage1" value="'; echo date_format($date, 'd/m/Y'); echo '">'; echo " "; echo '</div>'; echo '<div class="col span-1-of-4">'; echo '<input type="time" name="startTime[]" id="startTime[]">'; echo '</div>'; echo '<div class="col span-1-of-4">'; echo '<input type="time" name="endTime[]" id="endTime[]">'; echo '</div>'; echo '</div>'; } ?> The form works well. In another PHP I would like to use the three arrays workday[], startTime[] and endTime to calculate the duration between startTime and endTime for each working day. I can access the different arrays using Foreach like below but I don't see how to combine thos arrays in order to be able to calculate the duration between two data that are in diffrent arrays. Is it possible to make calculations using variables stored in different arrays ? If so could you help me understand how ? <?php if (isset($_POST['workday'])) { foreach ($_POST['workday'] as $i => $workday) { echo $workday . "<br>; } } if (isset($_POST['startTime'])) { foreach ($_POST['startTime'] as $i => $startTime) { echo $startTime. "<br>; } } if (isset($_POST['endTime'])) { foreach ($_POST['endTime'] as $i => $endTime) { echo $endTime. "<br>; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/311965-operations-with-variables-from-different-arrays/ Share on other sites More sharing options...
Barand Posted January 2, 2021 Share Posted January 2, 2021 (edited) One way would be to rename your input fields to something like name = 'jour[$jour][workday]' name = 'jour[$jour][startTime]' name = 'jour[$jour][endTime]' your posted data will then be nicely grouped Array ( [jour] => Array ( [1] => Array ( [workday] => 01/12/2020 [startTime] => 08:00 [endTime] => 06:00 ) [2] => Array ( [workday] => 02/12/2020 [startTime] => 08:30 [endTime] => 06:30 ) [3] => Array ( [workday] => 03/12/2020 [startTime] => 09:00 [endTime] => 04:30 ) . . . ) Edited January 2, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/311965-operations-with-variables-from-different-arrays/#findComment-1583614 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.