pods_serial_comma

Split an array into human readable text (Item, Item, and Item).

Description

(string) pods_serial_comma( (array) $value, (constant) $field = null, (constant) $fields = null, (constant) $and = null, (null) $field_index = null ); 

Returns (string)

Parameters (5)

0. $value (array)
The value.
1. $field — Optional. (constant) => null
The field.
2. $fields — Optional. (constant) => null
The fields.
3. $and — Optional. (constant) => null
The and.
4. $field_index — Optional. (null) => null
The field index.

Usage

  1. if ( !function_exists( 'pods_serial_comma' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'pods-custom-content-types-and-fields/includes/data.php'; 
  3.  
  4. // The value. 
  5. $value = array(); 
  6.  
  7. // The field. 
  8. $field = null; 
  9.  
  10. // The fields. 
  11. $fields = null; 
  12.  
  13. // The and. 
  14. $and = null; 
  15.  
  16. // The field index. 
  17. $field_index = null; 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = pods_serial_comma($value, $field, $fields, $and, $field_index); 
  21.  

Defined (1)

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

/includes/data.php  
  1. function pods_serial_comma ( $value, $field = null, $fields = null, $and = null, $field_index = null ) { 
  2. if ( is_object( $value ) ) 
  3. $value = get_object_vars( $value ); 
  4.  
  5. $defaults = array( 
  6. 'field' => $field,  
  7. 'fields' => $fields,  
  8. 'and' => $and,  
  9. 'field_index' => $field_index,  
  10. 'separator' => ', ',  
  11. 'serial' => true 
  12. ); 
  13.  
  14. if ( is_array( $field ) ) { 
  15. $defaults[ 'field' ] = null; 
  16.  
  17. $params = array_merge( $defaults, $field ); 
  18. else 
  19. $params = $defaults; 
  20.  
  21. $params = (object) $params; 
  22.  
  23. $simple = false; 
  24.  
  25. if ( !empty( $params->fields ) && is_array( $params->fields ) && isset( $params->fields[ $params->field ] ) ) { 
  26. $params->field = $params->fields[ $params->field ]; 
  27.  
  28. $simple_tableless_objects = PodsForm::simple_tableless_objects(); 
  29.  
  30. if ( !empty( $params->field ) && is_array( $params->field ) && in_array( $params->field[ 'type' ], PodsForm::tableless_field_types() ) ) { 
  31. if ( in_array( $params->field[ 'type' ], PodsForm::file_field_types() ) ) { 
  32. if ( null === $params->field_index ) 
  33. $params->field_index = 'guid'; 
  34. elseif ( in_array( $params->field[ 'pick_object' ], $simple_tableless_objects ) ) 
  35. $simple = true; 
  36. else { 
  37. $table = pods_api()->get_table_info( $params->field[ 'pick_object' ], $params->field[ 'pick_val' ], null, null, $params->field ); 
  38.  
  39. if ( !empty( $table ) ) { 
  40. if ( null === $params->field_index ) 
  41. $params->field_index = $table[ 'field_index' ]; 
  42. else 
  43. $params->field = null; 
  44.  
  45. if ( $simple && is_array( $params->field ) && !is_array( $value ) && '' !== $value && null !== $value ) 
  46. $value = PodsForm::field_method( 'pick', 'simple_value', $params->field[ 'name' ], $value, $params->field ); 
  47.  
  48. if ( !is_array( $value ) ) 
  49. return $value; 
  50.  
  51. if ( null === $params->and ) 
  52. $params->and = ' ' . __( 'and', pods ) . ' '; 
  53.  
  54. $last = ''; 
  55.  
  56. $original_value = $value; 
  57.  
  58. if ( !empty( $value ) ) 
  59. $last = array_pop( $value ); 
  60.  
  61. if ( $simple && is_array( $params->field ) && !is_array( $last ) && '' !== $last && null !== $last ) 
  62. $last = PodsForm::field_method( 'pick', 'simple_value', $params->field[ 'name' ], $last, $params->field ); 
  63.  
  64. if ( is_array( $last ) ) { 
  65. if ( null !== $params->field_index && isset( $last[ $params->field_index ] ) ) 
  66. $last = $last[ $params->field_index ]; 
  67. elseif ( isset( $last[ 0 ] ) ) 
  68. $last = $last[ 0 ]; 
  69. elseif ( $simple ) 
  70. $last = current( $last ); 
  71. else 
  72. $last = ''; 
  73.  
  74. if ( !empty( $value ) ) { 
  75. if ( null !== $params->field_index && isset( $original_value[ $params->field_index ] ) ) 
  76. return $original_value[ $params->field_index ]; 
  77. elseif ( null !== $params->field_index && isset( $value[ $params->field_index ] ) ) 
  78. return $value[ $params->field_index ]; 
  79. elseif ( !isset( $value[ 0 ] ) ) 
  80. $value = array( $value ); 
  81.  
  82. foreach ( $value as $k => $v ) { 
  83. if ( $simple && is_array( $params->field ) && !is_array( $v ) && '' !== $v && null !== $v ) 
  84. $v = PodsForm::field_method( 'pick', 'simple_value', $params->field[ 'name' ], $v, $params->field ); 
  85.  
  86. if ( is_array( $v ) ) { 
  87. if ( null !== $params->field_index && isset( $v[ $params->field_index ] ) ) 
  88. $v = $v[ $params->field_index ]; 
  89. elseif ( $simple ) 
  90. $v = trim( implode( $params->separator . ' ', $v ), $params->separator . ' ' ); 
  91. else { 
  92. unset( $value[ $k ] ); 
  93.  
  94. continue; 
  95.  
  96. $value[ $k ] = $v; 
  97.  
  98. if ( 1 == count( $value ) || !$params->serial ) 
  99. $value = trim( implode( $params->separator . ' ', $value ), $params->separator . ' ' ); 
  100. else 
  101. $value = trim( implode( $params->separator . ' ', $value ), $params->separator . ' ' ) . apply_filters( 'pods_serial_comma', $params->separator . ' ', $value, $original_value, $params ); 
  102.  
  103. $value = trim( $value ); 
  104. $last = trim( $last ); 
  105.  
  106. if ( 0 < strlen( $value ) && 0 < strlen( $last ) ) 
  107. $value = $value . $params->and . $last; 
  108. elseif ( 0 < strlen( $last ) ) 
  109. $value = $last; 
  110. else 
  111. $value = ''; 
  112. else 
  113. $value = $last; 
  114.  
  115. $value = trim( $value, $params->separator . ' ' ); 
  116.  
  117. $value = apply_filters( 'pods_serial_comma_value', $value, $original_value, $params ); 
  118.  
  119. return (string) $value;