'From Advanced Custom Field's checkbox, I need to add something as checked in the checkbox

this is what my code looks like: Any help will be very appreciable.

    <?php
        $btn = get_field('line_btn');
        if( $btn && in_array('study', $btn ) ) { 
          echo 'study';       // working well
        }
        elseif( $btn && in_array('help', $btn ) ) { 
          echo 'help';        // working well
        }
        elseif( $btn && in_array('study', 'help', $btn) ) { 
          echo 'both selected';     // not working
        }
      ?>


Solution 1:[1]

in_array() only checks for one value in the array and you are trying to pass in two. You would have to us two separate in_array() functions in the elseif.

<?php
        $btn = get_field('line_btn');
        if( $btn && in_array('study', $btn ) ) { 
          echo 'study';       // working well
        }
        elseif( $btn && in_array('help', $btn ) ) { 
          echo 'help';        // working well
        }
        elseif( $btn && in_array('study', $btn) && in_array( 'help', $btn ) { 
          echo 'both selected';
        }
      ?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JayDev95