I have created the following nested for loop in python - the idea is to take a int value for hours e.g 6 and then create an array of values from 1-6
hours = 6
hoursArray = [6]
convertHours = [] #Creating empty list
for i in hoursArray:
for j in range(i-1): #This will iterate till the value in list - 1
convertHours.append(j+1) #Appending values in new list
hoursList = convertHours + hoursArray
print(hoursList) #adding both the lists
output [1, 2, 3, 4, 5, 6]
Here is my attempt with php - I'm confused about the inner loop and how to do "for j in range(i-1)" in php also in php i should be getting the max value of the array into the range of the outer loop for($i = 0; $i <= $hoursArray-1; $i++) - again i'm not sure how to do this so i put in the int value $hours to test it.
<?php
$hours = 6;
$hoursArray = [$hours];
$convertHours =[];
for($i = 0; $i <= $hours-1; $i++) {
for($j = 0; $j <= $i-1; $j++) {
$convertHours = [$j+1];
$hoursList = array_merge($convertHours, $hoursArray);
}
}
var_dump($hoursList);
print_r($hoursList);
?>
Output array(2) { [0]=> int(5) [1]=> int(6) } Array ( [0] => 5 [1] => 6 )
Any help appreciated!!
Update: This solves the issue (although its not a nested loop)
<?php
$hours = 6;
$convertHours =[];
for($i = 0; $i <= $hours-1; $i++) {
$convertHours[] = $i+1;
}
var_dump($convertHours);
?>
OUPTPUT = array(6) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) }
foriterate overhoursArray(so only once) but in php you iterate overi < 5, is this a mistake by any chance ?