I'm new to php programming, so sorry for any errors whatsoever.
I have a $monthsKeys variable that contains an array going from 0 to 12. I have another array ($Months) that contains 12 months from December to January.
I'm trying to create a function that takes the array as parameter and gets the key value from $monthsKeys so that it can return the month's name contained in $Months.
function getnomMois($monthsKeys){
$monthName= array(['Decembre','Novembre','Octobre','Septembre','Aout','Juillet','Juin','Mai','Avril','Mars','Fevrier','Janvier']);
foreach($monthsKeys as $monthName){
$monthName[] = $monthKeys['numMois'];
}
}
What I expect the function to return:
Instead of 12 for the month's number it returns December
What I'm trying to do:
I try to get the key from the $monthsKeys array and use it to get the value from the $MonthName array
What I understand I need to do:
I must extract the key value from the 1rst array and use it to get the value of the 2nd array accordin to the 1rst array key.
ie: monthKey[12] shoudl allow me to return monthName[12]='December'.
Here is the explanation of your code (and the problem you had):
function getnomMois($monthsKeys){
$monthName= array(['Decembre','Novembre','Octobre','Septembre','Aout','Juillet','Juin','Mai','Avril','Mars','Fevrier','Janvier']);
//^ here $monthName is array
foreach($monthsKeys as $monthName){
//^ here you change $monthName to string
// becasuse you loop over the arrays and put each value
// inside the $monthName variabl
$monthName[] = $monthKeys['numMois'];
//^ here are you tring to do array operation,
// but the $monthName variable currently is a string (and you can't do that)
}
}