Support

Account

Home Forums ACF PRO Select field array problem…

Solved

Select field array problem…

  • Hi there!

    I’ve opened a support ticket and had no response yet so thought I would post here and see if anyone has any ideas!

    – I have a Custom Field that is a ‘Select’
    – In the admin it is setup for multiple values
    – For example one post only has one select field, and another post has multiple select fields
    – I understand how to show the first value from the array in the template

    – My problem is that as I echo out the first item from the array thats fine, but when there is just one item in the select field, it echo’s out the first letter as opposed to the whole field value.

    Here’s the code I have for the options field, I have tried adding an else field after to display a single item thats not an array, I cant get that to work. Is this a bug with ACF Select Field??

    <?php
    if(get_field('location') == "location");
    ?>
    
     <?php                 
     $values = get_field('location');
     if($values) {
     echo $values[0];
     }
    
    ?>

    Many thanks in advance if anyone has any ideas! Cheers
    Alex

  • It sounds like it is storing it as a string if there’s only one value. To test, you could try something like this:

    if ( is_array($values) ) {
        foreach($values as $val) {
            echo "<p>{$val}</p>";
        }
    } else {
        echo "<p>{$value}</p>";
    }
  • Hey there linkhousemedia, many thanks for your speedy reply! I’ll have a look now and post back.

    Cheers

  • Hey there!
    Thanks massively for the help, I worked it all out now.

    If this helps, here is my code for anyone else 🙂

    Field name is ‘Location’
    There are a lot of location stored as an array. To display the first one from the array (if there are multiple values) if just one value display that…

    if(get_field('location') == "location");
     $values = get_field('location');
                    if ( is_array($values) ) {
                        echo $values[0];
                    } else {
                    echo "$value";
                    }
    

    Thanks again! ]

  • I have ACF PRO version 5.8.7, and I still getting an array instead of a string from acf select fields when this is inside a flexible content. So I used designertastic conditional code to create a function and then used it on every select field.

    
    /**
     *
     * Array to string.
     *
     * This function is a workaround to ACF Select fields that sometimes output an array instead of a string.
     *
     * @param array $array Array value.
     * @return string value as string.
     */
    public function arr2str( $array ) {
    
    	if ( empty( $array ) ) {
    		return;
    	}
    
    	if ( is_array( $array ) ) {
    		$string = $array[0];
    	} else {
    		$string = $array;
    	}
    
    	return $string;
    }
    

    Usage

    
    $button_style = arr2str( $button['button_style'] );
    $button_style = arr2str( get_field( 'button_style' ) );
    $button_style = arr2str( get_sub_field( 'button_style' ) );
    
  • This is an old topic, but if anyone is interested in a cause.

    This usually happens when a select field is set one way, data is saved and then it is set the other way. If you have a select field that only allows one value then that value is stored as a string. If you have a select field that allows multiple values then that value is stored as an array. Updating the field does not alter how existing data is stored and if you have the field set to return values then you’ll get whatever is in the DB be it string or array.

    When you switch from one to another and there was already data stored then you need to account for it in your template where you are getting the value or optionally you could build an acf/load_value for the field and you can account for the change there.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Select field array problem…’ is closed to new replies.