Support

Account

Home Forums Add-ons Repeater Field Generate XML using sub_fields recursively Reply To: Generate XML using sub_fields recursively

  • Hi @bart-gopnik

    The easiest way would be checking if the current key is a number or not like this:

    // Create function that will loop through the array
    function print_array_value($value){
        if( is_array($value) ){
            foreach( $value as $k => $the_value ){
                
                if( is_numeric($k) ){echo '<' . $k . '>' . PHP_EOL;}
                
                print_array_value($the_value);
                
                if( is_numeric($k) ){echo '</' . $k . '>' . PHP_EOL;}
                
            }
        } else {
            echo $value . PHP_EOL;
        }
    }
    
    foreach($fields as $field){
        
        echo '<' . $field['name'] . '>' . PHP_EOL;
        
        // Check if this is a repeater or not
        print_array_value($field['value']);
        
        echo '</' . $field['name'] . '>' . PHP_EOL;
        
    }

    I hope this makes sense 🙂