I have arrays with the following names: $MyMondayClasses, $MyTuesdayClasses, $MyWednesdayClasses, $MyThursdayClasses, $MyFridayClasses
And I have an array for days of the week: $days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday');
I want to loop through the days of the week and then run the appropriate array, but I'm not sure how to refer to the variable to make:
foreach($days as $value){
foreach($My{$value}Classes as $ClassKey => $ClassVar){
some code goes here
}
}
I used to use eval for this sort of thing, but understand that's not best practice. Anyway, the above code isn't working.
I have arrays with the following names: $MyMondayClasses, $MyTuesdayClasses, $MyWednesdayClasses, $MyThursdayClasses, $MyFridayClasses
Sounds not very clever.
Why don't you just have one array, with 'monday', 'tuesday' etc. as keys - and then have your data arrays for each day under that key …?
Does not make sense .. you should use array but if you insist you can try :
foreach ( $days as $value ) {
$name = sprintf("My%sClasses", $value);
if (! isset(${$name})) { // the boss asked
continue;
}
foreach ( ${$name} as $ClassKey => $ClassVar ) {
// play some ball
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.