How to get list of all ACF fields?

Getting a list of all ACF fields may be useful e.g. if you want the user to select one of them as a data source. In my case, I needed that list to provide a choices list for an ACF select field.

This is an easy task if you know where to look :)

Getting a list of all ACF fields (example)

$options = array();

$field_groups = acf_get_field_groups();
foreach ( $field_groups as $group ) {  
  $fields = acf_get_fields($group['key']);
  foreach ( $fields as $field ) {
    $options[$field['name']] = $field['label'];
  }
}

As a result in the $options variable you will have an associative array of all the fields, labels keyed by the field name.

More details:

In the above example $field_groups variable is an array with all field groups. Here is the output of a var_dump($fied_groups):

array (size=3)  
  0 => 
    array (size=13)
      'ID' => int 1747
      'key' => string 'group_58f769278b155' (length=19)
      'title' => string 'Some title' (length=11)
      'fields' => 
        array (size=0)
          empty
      'location' => 
        array (size=1)
          0 => 
            array (size=1)
              ...
      'menu_order' => int 0
      'position' => string 'normal' (length=6)
      'style' => string 'default' (length=7)
      'label_placement' => string 'top' (length=3)
      'instruction_placement' => string 'label' (length=5)
      'hide_on_screen' => string '' (length=0)
      'active' => int 1
      'description' => string '' (length=0)
  1 => ...

If you want to get only fields which are defined by user/admin via the ACF editor you may filter all the field groups by ID. Groups created with ACF PHP API will have ID = 0.

Below is an output of var_dump($fields):

array (size=8)  
  0 => 
    array (size=23)
      'ID' => int 1748
      'key' => string 'field_58f76cbe5dda5' (length=19)
      'label' => string 'Name' (length=5)
      'name' => string 'name' (length=5)
      'prefix' => string 'acf' (length=3)
      'type' => string 'text' (length=4)
      'value' => null
      'menu_order' => int 0
      'instructions' => string '' (length=0)
      'required' => int 0
      'id' => string '' (length=0)
      'class' => string '' (length=0)
      'conditional_logic' => int 0
      'parent' => int 1747
      'wrapper' => 
        array (size=3)
          'width' => string '' (length=0)
          'class' => string '' (length=0)
          'id' => string '' (length=0)
      '_name' => string 'imię' (length=5)
      '_input' => string '' (length=0)
      '_valid' => int 1
      'default_value' => string '' (length=0)
      'placeholder' => string '' (length=0)
      'prepend' => string '' (length=0)
      'append' => string '' (length=0)
      'maxlength' => string '' (length=0)
  1 => 
    array (size=22)
      ...