I'm trying to break down a simple foreach loop into two functions: start and end.
functions.php :
function days_start() {
$days = [];
$meals_monday = get_field('monday_meal');
$meals_tuesday = get_field('tuesday_meal');
$meals_wednesday = get_field('wednesday_meal');
$meals_thursday = get_field('thursday_meal');
$meals_friday = get_field('friday_meal');
$meals_saturday = get_field('saturday_meal');
$meals_sunday = get_field('sunday_meal');
array_push($days, $meals_monday, $meals_tuesday, $meals_wednesday, $meals_thursday, $meals_friday, $meals_saturday, $meals_sunday);
foreach( $days as $day ):
}
function days_end() {
endforeach;
}
I do this so I can use it multiple times without rewriting it. However, I get this error:
"syntax error, unexpected '}'"
I think I must be blind, but I can't see why this would pop up. Or is it not possible to use a foreach function inside a function, without closing it?
Thanks!
Ok, I removed the foreach statement from the function and did this:
functions.php
function days_before_loop() {
global $days;
$days = [];
$meals_monday = get_field('monday_meal');
$meals_tuesday = get_field('tuesday_meal');
$meals_wednesday = get_field('wednesday_meal');
$meals_thursday = get_field('thursday_meal');
$meals_friday = get_field('friday_meal');
$meals_saturday = get_field('saturday_meal');
$meals_sunday = get_field('sunday_meal');
array_push($days, $meals_monday, $meals_tuesday, $meals_wednesday, $meals_thursday, $meals_friday, $meals_saturday, $meals_sunday);
return $days;
}
then call it like this:
days_before_loop();
foreach( $days as $day ):
// do some stuff
endforeach();
Thanks!