Is it possible to use php variable as a part of an array name. like this my arrays are Fday1, Fday2....but i cant go with the foreach keys beacause there are different counts of values for each array
$FdayArray = "Fday".$FdayKey;
array_push($FdayArray, $forecast);
the FdayKey would be a number between 1-9 How do I do this correctly?
You are looking for the variable variables
feature of php.
Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. It takes the value of a variable and treats that as the name of a variable
You would use
array_push($$FdayArray, $forecast);
Instead of maintaining different array for each $FdayKey
, you can have an associative array, which has internal arrays corresponds to each $FdayKey
The array can look like:
$FdayArray = [
'1' => [],
'2' => [],
'3' => [],
'4' => [],
'5' => [],
'6' => [],
'7' => [],
'8' => [],
'9' => []
];
When you need to push to the array, just use the $FdayKey
as a index to get the relevant array.
So you can push as:
array_push($FdayArray[$FdayKey], $forecast);