pods_hierarchical_select_recurse

Recurse list of hierarchical data.

Description

(array) pods_hierarchical_select_recurse( (array|object) $items, (array) $args, (int) $depth = 0 ); 

Returns (array)

Parameters (3)

0. $items (array|object)
List of items
1. $args (array)
Array of children and prefix keys to use
2. $depth — Optional. (int)
Key to recurse children into

Usage

  1. if ( !function_exists( 'pods_hierarchical_select_recurse' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'pods-custom-content-types-and-fields/includes/data.php'; 
  3.  
  4. // List of items 
  5. $items = null; 
  6.  
  7. // Array of children and prefix keys to use 
  8. $args = array(); 
  9.  
  10. // Key to recurse children into 
  11. $depth = -1; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = pods_hierarchical_select_recurse($items, $args, $depth); 
  15.  

Defined (1)

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

/includes/data.php  
  1. function pods_hierarchical_select_recurse ( $items, $args, $depth = 0 ) { 
  2. $data = array(); 
  3.  
  4. foreach ( $items as $k => $v ) { 
  5. $object = false; 
  6.  
  7. if ( is_object( $v ) ) { 
  8. $object = true; 
  9. $v = get_object_vars( $v ); 
  10.  
  11. if ( isset( $v[ $args[ 'index' ] ] ) ) 
  12. $v[ $args[ 'index' ] ] = ( 0 < $depth ? str_repeat( $args[ 'prefix' ], $depth ) : '' ) . $v[ $args[ 'index' ] ]; 
  13.  
  14. $children = array(); 
  15.  
  16. if ( isset( $v[ $args[ 'children' ] ] ) ) { 
  17. if ( !empty( $v[ $args[ 'children' ] ] ) ) 
  18. $children = pods_hierarchical_select_recurse( $v[ $args[ 'children' ] ], $args, ( $depth + 1 ) ); 
  19.  
  20. unset( $v[ $args[ 'children' ] ] ); 
  21.  
  22. if ( $object ) 
  23. $v = (object) $v; 
  24.  
  25. $data[ $k ] = $v; 
  26.  
  27. if ( !empty( $children ) ) { 
  28. foreach ( $children as $ck => $cv ) { 
  29. $data[ $ck ] = $cv; 
  30.  
  31. return $data;