acf_convert_field_names_to_keys

Acf_convert_field_names_to_keys().

Description

acf_convert_field_names_to_keys( $value, $field ); 

Helper for the update_field function

Parameters (2)

0. $value
The value.
1. $field
The field.

Usage

  1. if ( !function_exists( 'acf_convert_field_names_to_keys' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'advanced-custom-fields/core/api.php'; 
  3.  
  4. // The value. 
  5. $value = null; 
  6.  
  7. // The field. 
  8. $field = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = acf_convert_field_names_to_keys($value, $field); 
  12.  

Defined (1)

The function is defined in the following location(s).

/core/api.php  
  1. function acf_convert_field_names_to_keys( $value, $field ) 
  2. // only if $field has sub fields 
  3. if( !isset($field['sub_fields']) ) 
  4. return $value; 
  5.  
  6.  
  7. // define sub field keys 
  8. $sub_fields = array(); 
  9. if( $field['sub_fields'] ) 
  10. foreach( $field['sub_fields'] as $sub_field ) 
  11. $sub_fields[ $sub_field['name'] ] = $sub_field; 
  12.  
  13.  
  14. // loop through the values and format the array to use sub field keys 
  15. if( is_array($value) ) 
  16. foreach( $value as $row_i => $row) 
  17. if( $row ) 
  18. foreach( $row as $sub_field_name => $sub_field_value ) 
  19. // sub field must exist! 
  20. if( !isset($sub_fields[ $sub_field_name ]) ) 
  21. continue; 
  22.  
  23.  
  24. // vars 
  25. $sub_field = $sub_fields[ $sub_field_name ]; 
  26. $sub_field_value = acf_convert_field_names_to_keys( $sub_field_value, $sub_field ); 
  27.  
  28.  
  29. // set new value 
  30. $value[$row_i][ $sub_field['key'] ] = $sub_field_value; 
  31.  
  32.  
  33. // unset old value 
  34. unset( $value[$row_i][$sub_field_name] ); 
  35.  
  36.  
  37. // foreach( $row as $sub_field_name => $sub_field_value ) 
  38. // if( $row ) 
  39. // foreach( $value as $row_i => $row) 
  40. // if( $value ) 
  41.  
  42.  
  43. return $value; 
  44.