$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - How do I consolidate multiple variables into an array?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How do I consolidate multiple variables into an array?

matteradmin7PV0评论

All of this code works perfectly, if all of the acf fields have no data, the section isn't loaded. My problem is, the initial variable setup regarding $scheduleEventGroup1 and $scheduleEventGroup2 is long and I imagine there's a shorter way of doing this but I've tried foreach, arrays and other methods but can't get it to work. It only seems to work if I create a variable for each field for each group, one by one. 2 groups wouldn't be that big of a issue but eventually I'm going to have 10 groups. Is there a shorter way of doing this?

<?php 
                $scheduleEventGroup1 = get_field('cp_schedule_event_1');
                $scheduleEventGroup2 = get_field('cp_schedule_event_2');
                $scheduleEvents = array( $scheduleEventGroup1, $scheduleEventGroup2 );

                $scheduleEventImg1 = $scheduleEventGroup1['cp_schedule_event_image']; 
                $scheduleEventHour1 = $scheduleEventGroup1['cp_schedule_event_hour'];
                $scheduleEventDesc1 = $scheduleEventGroup1['cp_schedule_event_description'];
                
                $scheduleEventImg2 = $scheduleEventGroup2['cp_schedule_event_image']; 
                $scheduleEventHour2 = $scheduleEventGroup2['cp_schedule_event_hour'];
                $scheduleEventDesc2 = $scheduleEventGroup2['cp_schedule_event_description'];
                ?>
                                
                <?php                           
                if ( $scheduleEventImg1 || $scheduleEventHour1 || $scheduleEventDesc1 || $scheduleEventImg2 || $scheduleEventHour2 || $scheduleEventDesc2 ) { ?>

                    <section class="d-section" id="schedule">

                                <?php 
                                foreach ($scheduleEvents as $scheduleEvent) { 
                                    $scheduleEventImage = $scheduleEvent['cp_schedule_event_image'];
                                    $scheduleEventHour = $scheduleEvent['cp_schedule_event_hour'];
                                    $scheduleEventDescription = $scheduleEvent['cp_schedule_event_description'];

                                    if ( $scheduleEventImage || $scheduleEventHour || $scheduleEventDescription ) {     

                                        if ( $scheduleEventImage !== "" || $scheduleEventHour !== "" || $scheduleEventDescription !== "" ) { ?>

                                        <div class="d-block">
                                            <div class="block-wrapper">
                                                <img src="<?php echo $scheduleEventImage ?>" alt="">    
                                                <span class="hour d-font"><?php echo $scheduleEventHour ?></span>
                                                <span class="event"><?php echo $scheduleEventDescription ?></span>
                                            </div>
                                        </div>

                                        <?php }
                                    }

                                } ?>

                    </section>

                <?php } 
                ?>

All of this code works perfectly, if all of the acf fields have no data, the section isn't loaded. My problem is, the initial variable setup regarding $scheduleEventGroup1 and $scheduleEventGroup2 is long and I imagine there's a shorter way of doing this but I've tried foreach, arrays and other methods but can't get it to work. It only seems to work if I create a variable for each field for each group, one by one. 2 groups wouldn't be that big of a issue but eventually I'm going to have 10 groups. Is there a shorter way of doing this?

<?php 
                $scheduleEventGroup1 = get_field('cp_schedule_event_1');
                $scheduleEventGroup2 = get_field('cp_schedule_event_2');
                $scheduleEvents = array( $scheduleEventGroup1, $scheduleEventGroup2 );

                $scheduleEventImg1 = $scheduleEventGroup1['cp_schedule_event_image']; 
                $scheduleEventHour1 = $scheduleEventGroup1['cp_schedule_event_hour'];
                $scheduleEventDesc1 = $scheduleEventGroup1['cp_schedule_event_description'];
                
                $scheduleEventImg2 = $scheduleEventGroup2['cp_schedule_event_image']; 
                $scheduleEventHour2 = $scheduleEventGroup2['cp_schedule_event_hour'];
                $scheduleEventDesc2 = $scheduleEventGroup2['cp_schedule_event_description'];
                ?>
                                
                <?php                           
                if ( $scheduleEventImg1 || $scheduleEventHour1 || $scheduleEventDesc1 || $scheduleEventImg2 || $scheduleEventHour2 || $scheduleEventDesc2 ) { ?>

                    <section class="d-section" id="schedule">

                                <?php 
                                foreach ($scheduleEvents as $scheduleEvent) { 
                                    $scheduleEventImage = $scheduleEvent['cp_schedule_event_image'];
                                    $scheduleEventHour = $scheduleEvent['cp_schedule_event_hour'];
                                    $scheduleEventDescription = $scheduleEvent['cp_schedule_event_description'];

                                    if ( $scheduleEventImage || $scheduleEventHour || $scheduleEventDescription ) {     

                                        if ( $scheduleEventImage !== "" || $scheduleEventHour !== "" || $scheduleEventDescription !== "" ) { ?>

                                        <div class="d-block">
                                            <div class="block-wrapper">
                                                <img src="<?php echo $scheduleEventImage ?>" alt="">    
                                                <span class="hour d-font"><?php echo $scheduleEventHour ?></span>
                                                <span class="event"><?php echo $scheduleEventDescription ?></span>
                                            </div>
                                        </div>

                                        <?php }
                                    }

                                } ?>

                    </section>

                <?php } 
                ?>
Share Improve this question edited yesterday fuxia 107k39 gold badges255 silver badges461 bronze badges asked yesterday alexalex 32 bronze badges New contributor alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 1

you can test that when you construct the list $scheduleEvents like that :

<?php

$groups = [
    "cp_schedule_event_1",
    "cp_schedule_event_2",
    "cp_schedule_event_3",
    // ... other groups names here
];


$scheduleEvents = [];


foreach ($groups as $groupe_name) {
    
    $scheduleEvent = get_field($groupe_name);
    
    $scheduleEventImg = $scheduleEvent['cp_schedule_event_image']; 
    $scheduleEventHour = $scheduleEvent['cp_schedule_event_hour'];
    $scheduleEventDesc = $scheduleEvent['cp_schedule_event_description'];
    
    if (    !empty($scheduleEventImg)
        ||  !empty($scheduleEventHour)
        ||  !empty($scheduleEventDesc)
    ) {
        $scheduleEvents[] = $scheduleEvent;
    }
    
}


?>
<section class="d-section" id="schedule">

    <?php foreach ($scheduleEvents as $scheduleEvent) {?>
        
        <?php
        
        $scheduleEventImage = $scheduleEvent['cp_schedule_event_image'];
        $scheduleEventHour = $scheduleEvent['cp_schedule_event_hour'];
        $scheduleEventDescription = $scheduleEvent['cp_schedule_event_description'];
        
        ?>
            <div class="d-block">
                <div class="block-wrapper">
                    <img src="<?= htmlspecialchars($scheduleEventImage)?>" alt=""/>
                    <span class="hour d-font">
                        <?= htmlspecialchars($scheduleEventHour)?>
                    </span>
                    <span class="event">
                        <?= htmlspecialchars($scheduleEventDescription)?>
                    </span>
                </div>
            </div>
        <?php
        
    } ?>

</section>
Post a comment

comment list (0)

  1. No comments so far