Support

Account

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

Solving

Generate XML using sub_fields recursively

  • Hi!

    I have the following fields and subfields:

    -field_1->value_1
    -field_2->
    --subfield_1->value_2
    --subfield_2->value_3
    -field_3->subfield_3->subfield_4->subfield_5->value_4
    ...
    
    etc.

    I need to generate XML using names of fields and subfields and values like this:

    <field_1>value_1</field_1>
    <field_2>
      <subfield_1>value_2</subfield_1>
      <subfield_2>value_3</subfield_2>
    </field_2>
    <field_3>
      <subfield_3>
        <subfield_4>
         <subfield_5>
           value_4
         </subfield_5>
        </subfield_4>
      </subfield_3>
    </field_3>

    Here is my code:

    <?php
    
    $args = array(
    	'post_type' => 'kvartiry',
    );
    
    $objects = new WP_Query($args);
    
    while ($objects -> have_posts()) : $objects -> the_post();
    
    // $objectID = $post->ID;
    
    $fields = get_field_objects();
    
    //var_dump( $fields );
    
    if($fields)
    {
    	foreach($fields as $field_name => $field)
    	{
    
    		echo '<' . $field['name'] . '>' . PHP_EOL;
    
    		while(has_sub_field($field['name'])):
    
    			foreach($field['sub_fields'] as $k => $v)
    			{
    				echo '<' . $v['name']. '>' . get_sub_field($v['name']) . '</' . $v['name']. '>' . PHP_EOL;
    			}
    
    		endwhile;
    
    		echo '</' . $field['name'] . '>' . PHP_EOL;
    
    	}
    }
    
    endwhile;
    
    wp_reset_postdata();
    
    ?>

    Here is the output:

    <rooms>
    </rooms>
    <floor>
    </floor>
    <ololo>
    <lola>qwerty</lola>
    <lola>zaq</lola>
    </ololo>
    <azaza>
    <zaza>13777777</zaza>
    <zaza>44444555</zaza>
    </azaza>
    <rooms>
    </rooms>
    <floor>
    </floor>
    <ololo>
    <lola>777</lola>
    </ololo>
    <azaza>
    </azaza>
    <level_1>
    <level_2>Array</level_2>
    </level_1>
    <rooms>
    </rooms>
    <floor>
    </floor>
    <rooms>
    </rooms>
    <floor>
    </floor>

    Here is the output with var_dump( $fields ) uncommented:

    array(4) {
      ["rooms"]=>
      array(26) {
        ["ID"]=>
        int(7398)
        ["key"]=>
        string(19) "field_5806147563ebb"
        ["label"]=>
        string(33) "Количество комнат"
        ["name"]=>
        string(5) "rooms"
        ["prefix"]=>
        string(0) ""
        ["type"]=>
        string(4) "text"
        ["value"]=>
        string(1) "a"
        ["menu_order"]=>
        int(0)
        ["instructions"]=>
        string(0) ""
        ["required"]=>
        int(0)
        ["id"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["conditional_logic"]=>
        int(0)
        ["parent"]=>
        int(7397)
        ["wrapper"]=>
        array(3) {
          ["width"]=>
          string(0) ""
          ["class"]=>
          string(0) ""
          ["id"]=>
          string(0) ""
        }
        ["_name"]=>
        string(5) "rooms"
        ["_input"]=>
        string(0) ""
        ["_valid"]=>
        int(1)
        ["default_value"]=>
        string(0) ""
        ["placeholder"]=>
        string(0) ""
        ["prepend"]=>
        string(0) ""
        ["append"]=>
        string(0) ""
        ["formatting"]=>
        string(4) "html"
        ["maxlength"]=>
        string(0) ""
        ["readonly"]=>
        int(0)
        ["disabled"]=>
        int(0)
      }
      ["floor"]=>
      array(26) {
        ["ID"]=>
        int(7399)
        ["key"]=>
        string(19) "field_580615f0c1feb"
        ["label"]=>
        string(8) "Этаж"
        ["name"]=>
        string(5) "floor"
        ["prefix"]=>
        string(0) ""
        ["type"]=>
        string(4) "text"
        ["value"]=>
        string(1) "b"
        ["menu_order"]=>
        int(1)
        ["instructions"]=>
        string(0) ""
        ["required"]=>
        int(0)
        ["id"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["conditional_logic"]=>
        int(0)
        ["parent"]=>
        int(7397)
        ["wrapper"]=>
        array(3) {
          ["width"]=>
          string(0) ""
          ["class"]=>
          string(0) ""
          ["id"]=>
          string(0) ""
        }
        ["_name"]=>
        string(5) "floor"
        ["_input"]=>
        string(0) ""
        ["_valid"]=>
        int(1)
        ["default_value"]=>
        string(0) ""
        ["placeholder"]=>
        string(0) ""
        ["prepend"]=>
        string(0) ""
        ["append"]=>
        string(0) ""
        ["formatting"]=>
        string(4) "html"
        ["maxlength"]=>
        string(0) ""
        ["readonly"]=>
        int(0)
        ["disabled"]=>
        int(0)
      }
      ["ololo"]=>
      array(24) {
        ["ID"]=>
        int(7547)
        ["key"]=>
        string(19) "field_5809c398a3d38"
        ["label"]=>
        string(10) "Ололо"
        ["name"]=>
        string(5) "ololo"
        ["prefix"]=>
        string(0) ""
        ["type"]=>
        string(8) "repeater"
        ["value"]=>
        array(2) {
          [0]=>
          array(1) {
            ["lola"]=>
            string(6) "qwerty"
          }
          [1]=>
          array(1) {
            ["lola"]=>
            string(3) "zaq"
          }
        }
        ["menu_order"]=>
        int(3)
        ["instructions"]=>
        string(0) ""
        ["required"]=>
        int(0)
        ["id"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["conditional_logic"]=>
        int(0)
        ["parent"]=>
        int(7397)
        ["wrapper"]=>
        array(3) {
          ["width"]=>
          string(0) ""
          ["class"]=>
          string(0) ""
          ["id"]=>
          string(0) ""
        }
        ["_name"]=>
        string(5) "ololo"
        ["_input"]=>
        string(0) ""
        ["_valid"]=>
        int(1)
        ["collapsed"]=>
        string(0) ""
        ["min"]=>
        string(0) ""
        ["max"]=>
        string(0) ""
        ["layout"]=>
        string(5) "table"
        ["button_label"]=>
        string(16) "Добавить"
        ["sub_fields"]=>
        array(1) {
          [0]=>
          array(25) {
            ["ID"]=>
            int(7548)
            ["key"]=>
            string(19) "field_5809c3b9a3d39"
            ["label"]=>
            string(8) "Лола"
            ["name"]=>
            string(4) "lola"
            ["prefix"]=>
            string(0) ""
            ["type"]=>
            string(4) "text"
            ["value"]=>
            NULL
            ["menu_order"]=>
            int(0)
            ["instructions"]=>
            string(0) ""
            ["required"]=>
            int(0)
            ["id"]=>
            string(0) ""
            ["class"]=>
            string(0) ""
            ["conditional_logic"]=>
            int(0)
            ["parent"]=>
            int(7547)
            ["wrapper"]=>
            array(3) {
              ["width"]=>
              string(0) ""
              ["class"]=>
              string(0) ""
              ["id"]=>
              string(0) ""
            }
            ["_name"]=>
            string(4) "lola"
            ["_input"]=>
            string(0) ""
            ["_valid"]=>
            int(1)
            ["default_value"]=>
            string(0) ""
            ["placeholder"]=>
            string(0) ""
            ["prepend"]=>
            string(0) ""
            ["append"]=>
            string(0) ""
            ["maxlength"]=>
            string(0) ""
            ["readonly"]=>
            int(0)
            ["disabled"]=>
            int(0)
          }
        }
      }
      ["azaza"]=>
      array(24) {
        ["ID"]=>
        int(7544)
        ["key"]=>
        string(19) "field_5809b90943369"
        ["label"]=>
        string(10) "Азаза"
        ["name"]=>
        string(5) "azaza"
        ["prefix"]=>
        string(0) ""
        ["type"]=>
        string(8) "repeater"
        ["value"]=>
        array(2) {
          [0]=>
          array(1) {
            ["zaza"]=>
            string(8) "13777777"
          }
          [1]=>
          array(1) {
            ["zaza"]=>
            string(8) "44444555"
          }
        }
        ["menu_order"]=>
        int(2)
        ["instructions"]=>
        string(0) ""
        ["required"]=>
        int(0)
        ["id"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["conditional_logic"]=>
        int(0)
        ["parent"]=>
        int(7397)
        ["wrapper"]=>
        array(3) {
          ["width"]=>
          string(0) ""
          ["class"]=>
          string(0) ""
          ["id"]=>
          string(0) ""
        }
        ["_name"]=>
        string(5) "azaza"
        ["_input"]=>
        string(0) ""
        ["_valid"]=>
        int(1)
        ["collapsed"]=>
        string(0) ""
        ["min"]=>
        string(0) ""
        ["max"]=>
        string(0) ""
        ["layout"]=>
        string(5) "table"
        ["button_label"]=>
        string(16) "Добавить"
        ["sub_fields"]=>
        array(1) {
          [0]=>
          array(27) {
            ["ID"]=>
            int(7545)
            ["key"]=>
            string(19) "field_5809b9844336c"
            ["label"]=>
            string(4) "Zaza"
            ["name"]=>
            string(4) "zaza"
            ["prefix"]=>
            string(0) ""
            ["type"]=>
            string(6) "number"
            ["value"]=>
            NULL
            ["menu_order"]=>
            int(0)
            ["instructions"]=>
            string(0) ""
            ["required"]=>
            int(0)
            ["id"]=>
            string(0) ""
            ["class"]=>
            string(0) ""
            ["conditional_logic"]=>
            int(0)
            ["parent"]=>
            int(7544)
            ["wrapper"]=>
            array(3) {
              ["width"]=>
              string(0) ""
              ["class"]=>
              string(0) ""
              ["id"]=>
              string(0) ""
            }
            ["_name"]=>
            string(4) "zaza"
            ["_input"]=>
            string(0) ""
            ["_valid"]=>
            int(1)
            ["default_value"]=>
            int(556)
            ["placeholder"]=>
            string(0) ""
            ["prepend"]=>
            string(0) ""
            ["append"]=>
            string(0) ""
            ["min"]=>
            string(0) ""
            ["max"]=>
            string(0) ""
            ["step"]=>
            string(0) ""
            ["readonly"]=>
            int(0)
            ["disabled"]=>
            int(0)
          }
        }
      }
    }
    <rooms>
    </rooms>
    <floor>
    </floor>
    <ololo>
    <lola>qwerty</lola>
    <lola>zaq</lola>
    </ololo>
    <azaza>
    <zaza>13777777</zaza>
    <zaza>44444555</zaza>
    </azaza>
    array(5) {
      ["rooms"]=>
      array(26) {
        ["ID"]=>
        int(7398)
        ["key"]=>
        string(19) "field_5806147563ebb"
        ["label"]=>
        string(33) "Количество комнат"
        ["name"]=>
        string(5) "rooms"
        ["prefix"]=>
        string(0) ""
        ["type"]=>
        string(4) "text"
        ["value"]=>
        string(1) "5"
        ["menu_order"]=>
        int(0)
        ["instructions"]=>
        string(0) ""
        ["required"]=>
        int(0)
        ["id"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["conditional_logic"]=>
        int(0)
        ["parent"]=>
        int(7397)
        ["wrapper"]=>
        array(3) {
          ["width"]=>
          string(0) ""
          ["class"]=>
          string(0) ""
          ["id"]=>
          string(0) ""
        }
        ["_name"]=>
        string(5) "rooms"
        ["_input"]=>
        string(0) ""
        ["_valid"]=>
        int(1)
        ["default_value"]=>
        string(0) ""
        ["placeholder"]=>
        string(0) ""
        ["prepend"]=>
        string(0) ""
        ["append"]=>
        string(0) ""
        ["formatting"]=>
        string(4) "html"
        ["maxlength"]=>
        string(0) ""
        ["readonly"]=>
        int(0)
        ["disabled"]=>
        int(0)
      }
      ["floor"]=>
      array(26) {
        ["ID"]=>
        int(7399)
        ["key"]=>
        string(19) "field_580615f0c1feb"
        ["label"]=>
        string(8) "Этаж"
        ["name"]=>
        string(5) "floor"
        ["prefix"]=>
        string(0) ""
        ["type"]=>
        string(4) "text"
        ["value"]=>
        string(1) "3"
        ["menu_order"]=>
        int(1)
        ["instructions"]=>
        string(0) ""
        ["required"]=>
        int(0)
        ["id"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["conditional_logic"]=>
        int(0)
        ["parent"]=>
        int(7397)
        ["wrapper"]=>
        array(3) {
          ["width"]=>
          string(0) ""
          ["class"]=>
          string(0) ""
          ["id"]=>
          string(0) ""
        }
        ["_name"]=>
        string(5) "floor"
        ["_input"]=>
        string(0) ""
        ["_valid"]=>
        int(1)
        ["default_value"]=>
        string(0) ""
        ["placeholder"]=>
        string(0) ""
        ["prepend"]=>
        string(0) ""
        ["append"]=>
        string(0) ""
        ["formatting"]=>
        string(4) "html"
        ["maxlength"]=>
        string(0) ""
        ["readonly"]=>
        int(0)
        ["disabled"]=>
        int(0)
      }
      ["ololo"]=>
      array(24) {
        ["ID"]=>
        int(7547)
        ["key"]=>
        string(19) "field_5809c398a3d38"
        ["label"]=>
        string(10) "Ололо"
        ["name"]=>
        string(5) "ololo"
        ["prefix"]=>
        string(0) ""
        ["type"]=>
        string(8) "repeater"
        ["value"]=>
        array(1) {
          [0]=>
          array(1) {
            ["lola"]=>
            string(3) "777"
          }
        }
        ["menu_order"]=>
        int(3)
        ["instructions"]=>
        string(0) ""
        ["required"]=>
        int(0)
        ["id"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["conditional_logic"]=>
        int(0)
        ["parent"]=>
        int(7397)
        ["wrapper"]=>
        array(3) {
          ["width"]=>
          string(0) ""
          ["class"]=>
          string(0) ""
          ["id"]=>
          string(0) ""
        }
        ["_name"]=>
        string(5) "ololo"
        ["_input"]=>
        string(0) ""
        ["_valid"]=>
        int(1)
        ["collapsed"]=>
        string(0) ""
        ["min"]=>
        string(0) ""
        ["max"]=>
        string(0) ""
        ["layout"]=>
        string(5) "table"
        ["button_label"]=>
        string(16) "Добавить"
        ["sub_fields"]=>
        array(1) {
          [0]=>
          array(25) {
            ["ID"]=>
            int(7548)
            ["key"]=>
            string(19) "field_5809c3b9a3d39"
            ["label"]=>
            string(8) "Лола"
            ["name"]=>
            string(4) "lola"
            ["prefix"]=>
            string(0) ""
            ["type"]=>
            string(4) "text"
            ["value"]=>
            NULL
            ["menu_order"]=>
            int(0)
            ["instructions"]=>
            string(0) ""
            ["required"]=>
            int(0)
            ["id"]=>
            string(0) ""
            ["class"]=>
            string(0) ""
            ["conditional_logic"]=>
            int(0)
            ["parent"]=>
            int(7547)
            ["wrapper"]=>
            array(3) {
              ["width"]=>
              string(0) ""
              ["class"]=>
              string(0) ""
              ["id"]=>
              string(0) ""
            }
            ["_name"]=>
            string(4) "lola"
            ["_input"]=>
            string(0) ""
            ["_valid"]=>
            int(1)
            ["default_value"]=>
            string(0) ""
            ["placeholder"]=>
            string(0) ""
            ["prepend"]=>
            string(0) ""
            ["append"]=>
            string(0) ""
            ["maxlength"]=>
            string(0) ""
            ["readonly"]=>
            int(0)
            ["disabled"]=>
            int(0)
          }
        }
      }
      ["azaza"]=>
      array(24) {
        ["ID"]=>
        int(7544)
        ["key"]=>
        string(19) "field_5809b90943369"
        ["label"]=>
        string(10) "Азаза"
        ["name"]=>
        string(5) "azaza"
        ["prefix"]=>
        string(0) ""
        ["type"]=>
        string(8) "repeater"
        ["value"]=>
        bool(false)
        ["menu_order"]=>
        int(2)
        ["instructions"]=>
        string(0) ""
        ["required"]=>
        int(0)
        ["id"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["conditional_logic"]=>
        int(0)
        ["parent"]=>
        int(7397)
        ["wrapper"]=>
        array(3) {
          ["width"]=>
          string(0) ""
          ["class"]=>
          string(0) ""
          ["id"]=>
          string(0) ""
        }
        ["_name"]=>
        string(5) "azaza"
        ["_input"]=>
        string(0) ""
        ["_valid"]=>
        int(1)
        ["collapsed"]=>
        string(0) ""
        ["min"]=>
        string(0) ""
        ["max"]=>
        string(0) ""
        ["layout"]=>
        string(5) "table"
        ["button_label"]=>
        string(16) "Добавить"
        ["sub_fields"]=>
        array(1) {
          [0]=>
          array(27) {
            ["ID"]=>
            int(7545)
            ["key"]=>
            string(19) "field_5809b9844336c"
            ["label"]=>
            string(4) "Zaza"
            ["name"]=>
            string(4) "zaza"
            ["prefix"]=>
            string(0) ""
            ["type"]=>
            string(6) "number"
            ["value"]=>
            NULL
            ["menu_order"]=>
            int(0)
            ["instructions"]=>
            string(0) ""
            ["required"]=>
            int(0)
            ["id"]=>
            string(0) ""
            ["class"]=>
            string(0) ""
            ["conditional_logic"]=>
            int(0)
            ["parent"]=>
            int(7544)
            ["wrapper"]=>
            array(3) {
              ["width"]=>
              string(0) ""
              ["class"]=>
              string(0) ""
              ["id"]=>
              string(0) ""
            }
            ["_name"]=>
            string(4) "zaza"
            ["_input"]=>
            string(0) ""
            ["_valid"]=>
            int(1)
            ["default_value"]=>
            int(556)
            ["placeholder"]=>
            string(0) ""
            ["prepend"]=>
            string(0) ""
            ["append"]=>
            string(0) ""
            ["min"]=>
            string(0) ""
            ["max"]=>
            string(0) ""
            ["step"]=>
            string(0) ""
            ["readonly"]=>
            int(0)
            ["disabled"]=>
            int(0)
          }
        }
      }
      ["level_1"]=>
      array(24) {
        ["ID"]=>
        int(7549)
        ["key"]=>
        string(19) "field_5809c4de3c0e0"
        ["label"]=>
        string(16) "Уровень 1"
        ["name"]=>
        string(7) "level_1"
        ["prefix"]=>
        string(0) ""
        ["type"]=>
        string(8) "repeater"
        ["value"]=>
        array(1) {
          [0]=>
          array(1) {
            ["level_2"]=>
            array(1) {
              [0]=>
              array(1) {
                ["level_3"]=>
                string(7) "ggggggg"
              }
            }
          }
        }
        ["menu_order"]=>
        int(4)
        ["instructions"]=>
        string(0) ""
        ["required"]=>
        int(0)
        ["id"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["conditional_logic"]=>
        int(0)
        ["parent"]=>
        int(7397)
        ["wrapper"]=>
        array(3) {
          ["width"]=>
          string(0) ""
          ["class"]=>
          string(0) ""
          ["id"]=>
          string(0) ""
        }
        ["_name"]=>
        string(7) "level_1"
        ["_input"]=>
        string(0) ""
        ["_valid"]=>
        int(1)
        ["collapsed"]=>
        string(0) ""
        ["min"]=>
        string(0) ""
        ["max"]=>
        string(0) ""
        ["layout"]=>
        string(5) "table"
        ["button_label"]=>
        string(16) "Добавить"
        ["sub_fields"]=>
        array(1) {
          [0]=>
          array(24) {
            ["ID"]=>
            int(7550)
            ["key"]=>
            string(19) "field_5809c52f3c0e1"
            ["label"]=>
            string(16) "Уровень 2"
            ["name"]=>
            string(7) "level_2"
            ["prefix"]=>
            string(0) ""
            ["type"]=>
            string(8) "repeater"
            ["value"]=>
            NULL
            ["menu_order"]=>
            int(0)
            ["instructions"]=>
            string(0) ""
            ["required"]=>
            int(0)
            ["id"]=>
            string(0) ""
            ["class"]=>
            string(0) ""
            ["conditional_logic"]=>
            int(0)
            ["parent"]=>
            int(7549)
            ["wrapper"]=>
            array(3) {
              ["width"]=>
              string(0) ""
              ["class"]=>
              string(0) ""
              ["id"]=>
              string(0) ""
            }
            ["_name"]=>
            string(7) "level_2"
            ["_input"]=>
            string(0) ""
            ["_valid"]=>
            int(1)
            ["collapsed"]=>
            string(0) ""
            ["min"]=>
            string(0) ""
            ["max"]=>
            string(0) ""
            ["layout"]=>
            string(5) "table"
            ["button_label"]=>
            string(16) "Добавить"
            ["sub_fields"]=>
            array(1) {
              [0]=>
              array(25) {
                ["ID"]=>
                int(7551)
                ["key"]=>
                string(19) "field_5809c5513c0e2"
                ["label"]=>
                string(16) "Уровень 3"
                ["name"]=>
                string(7) "level_3"
                ["prefix"]=>
                string(0) ""
                ["type"]=>
                string(4) "text"
                ["value"]=>
                NULL
                ["menu_order"]=>
                int(0)
                ["instructions"]=>
                string(0) ""
                ["required"]=>
                int(0)
                ["id"]=>
                string(0) ""
                ["class"]=>
                string(0) ""
                ["conditional_logic"]=>
                int(0)
                ["parent"]=>
                int(7550)
                ["wrapper"]=>
                array(3) {
                  ["width"]=>
                  string(0) ""
                  ["class"]=>
                  string(0) ""
                  ["id"]=>
                  string(0) ""
                }
                ["_name"]=>
                string(7) "level_3"
                ["_input"]=>
                string(0) ""
                ["_valid"]=>
                int(1)
                ["default_value"]=>
                string(0) ""
                ["placeholder"]=>
                string(0) ""
                ["prepend"]=>
                string(0) ""
                ["append"]=>
                string(0) ""
                ["maxlength"]=>
                string(0) ""
                ["readonly"]=>
                int(0)
                ["disabled"]=>
                int(0)
              }
            }
          }
        }
      }
    }
    <rooms>
    </rooms>
    <floor>
    </floor>
    <ololo>
    <lola>777</lola>
    </ololo>
    <azaza>
    </azaza>
    <level_1>
    <level_2>Array</level_2>
    </level_1>
    array(2) {
      ["rooms"]=>
      array(26) {
        ["ID"]=>
        int(7398)
        ["key"]=>
        string(19) "field_5806147563ebb"
        ["label"]=>
        string(33) "Количество комнат"
        ["name"]=>
        string(5) "rooms"
        ["prefix"]=>
        string(0) ""
        ["type"]=>
        string(4) "text"
        ["value"]=>
        string(1) "7"
        ["menu_order"]=>
        int(0)
        ["instructions"]=>
        string(0) ""
        ["required"]=>
        int(0)
        ["id"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["conditional_logic"]=>
        int(0)
        ["parent"]=>
        int(7397)
        ["wrapper"]=>
        array(3) {
          ["width"]=>
          string(0) ""
          ["class"]=>
          string(0) ""
          ["id"]=>
          string(0) ""
        }
        ["_name"]=>
        string(5) "rooms"
        ["_input"]=>
        string(0) ""
        ["_valid"]=>
        int(1)
        ["default_value"]=>
        string(0) ""
        ["placeholder"]=>
        string(0) ""
        ["prepend"]=>
        string(0) ""
        ["append"]=>
        string(0) ""
        ["formatting"]=>
        string(4) "html"
        ["maxlength"]=>
        string(0) ""
        ["readonly"]=>
        int(0)
        ["disabled"]=>
        int(0)
      }
      ["floor"]=>
      array(26) {
        ["ID"]=>
        int(7399)
        ["key"]=>
        string(19) "field_580615f0c1feb"
        ["label"]=>
        string(8) "Этаж"
        ["name"]=>
        string(5) "floor"
        ["prefix"]=>
        string(0) ""
        ["type"]=>
        string(4) "text"
        ["value"]=>
        string(2) "10"
        ["menu_order"]=>
        int(1)
        ["instructions"]=>
        string(0) ""
        ["required"]=>
        int(0)
        ["id"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["conditional_logic"]=>
        int(0)
        ["parent"]=>
        int(7397)
        ["wrapper"]=>
        array(3) {
          ["width"]=>
          string(0) ""
          ["class"]=>
          string(0) ""
          ["id"]=>
          string(0) ""
        }
        ["_name"]=>
        string(5) "floor"
        ["_input"]=>
        string(0) ""
        ["_valid"]=>
        int(1)
        ["default_value"]=>
        string(0) ""
        ["placeholder"]=>
        string(0) ""
        ["prepend"]=>
        string(0) ""
        ["append"]=>
        string(0) ""
        ["formatting"]=>
        string(4) "html"
        ["maxlength"]=>
        string(0) ""
        ["readonly"]=>
        int(0)
        ["disabled"]=>
        int(0)
      }
    }
    <rooms>
    </rooms>
    <floor>
    </floor>
    array(2) {
      ["rooms"]=>
      array(26) {
        ["ID"]=>
        int(7398)
        ["key"]=>
        string(19) "field_5806147563ebb"
        ["label"]=>
        string(33) "Количество комнат"
        ["name"]=>
        string(5) "rooms"
        ["prefix"]=>
        string(0) ""
        ["type"]=>
        string(4) "text"
        ["value"]=>
        string(1) "1"
        ["menu_order"]=>
        int(0)
        ["instructions"]=>
        string(0) ""
        ["required"]=>
        int(0)
        ["id"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["conditional_logic"]=>
        int(0)
        ["parent"]=>
        int(7397)
        ["wrapper"]=>
        array(3) {
          ["width"]=>
          string(0) ""
          ["class"]=>
          string(0) ""
          ["id"]=>
          string(0) ""
        }
        ["_name"]=>
        string(5) "rooms"
        ["_input"]=>
        string(0) ""
        ["_valid"]=>
        int(1)
        ["default_value"]=>
        string(0) ""
        ["placeholder"]=>
        string(0) ""
        ["prepend"]=>
        string(0) ""
        ["append"]=>
        string(0) ""
        ["formatting"]=>
        string(4) "html"
        ["maxlength"]=>
        string(0) ""
        ["readonly"]=>
        int(0)
        ["disabled"]=>
        int(0)
      }
      ["floor"]=>
      array(26) {
        ["ID"]=>
        int(7399)
        ["key"]=>
        string(19) "field_580615f0c1feb"
        ["label"]=>
        string(8) "Этаж"
        ["name"]=>
        string(5) "floor"
        ["prefix"]=>
        string(0) ""
        ["type"]=>
        string(4) "text"
        ["value"]=>
        string(1) "2"
        ["menu_order"]=>
        int(1)
        ["instructions"]=>
        string(0) ""
        ["required"]=>
        int(0)
        ["id"]=>
        string(0) ""
        ["class"]=>
        string(0) ""
        ["conditional_logic"]=>
        int(0)
        ["parent"]=>
        int(7397)
        ["wrapper"]=>
        array(3) {
          ["width"]=>
          string(0) ""
          ["class"]=>
          string(0) ""
          ["id"]=>
          string(0) ""
        }
        ["_name"]=>
        string(5) "floor"
        ["_input"]=>
        string(0) ""
        ["_valid"]=>
        int(1)
        ["default_value"]=>
        string(0) ""
        ["placeholder"]=>
        string(0) ""
        ["prepend"]=>
        string(0) ""
        ["append"]=>
        string(0) ""
        ["formatting"]=>
        string(4) "html"
        ["maxlength"]=>
        string(0) ""
        ["readonly"]=>
        int(0)
        ["disabled"]=>
        int(0)
      }
    }
    <rooms>
    </rooms>
    <floor>
    </floor>

    The problem is <level_2>Array</level_2>. Should be <level_2><level_3>ggggggg</level_3></level_2>

    How to iterate this nested object (array) recursively? Should I use has_sub_field() or have_rows()? This nested object (array) may be long deep.

  • How to iterate this nested object (array) recursively? Should I use has_sub_field() or have_rows()? This nested object (array) may be long deep.

  • Hi @bart-gopnik

    In this case, you need to create a function to loop through the returned fields and its values. Here’s an example how to do it:

    // Create function that will loop through the array
    function print_array_value($value){
        if( is_array($value) ){
            foreach( $value as $k => $the_value ){
                echo '<' . $k . '>' . PHP_EOL;
                print_array_value($the_value);
                echo '</' . $k . '>' . PHP_EOL;
            }
        } else {
            echo $value . PHP_EOL;
        }
    }
    
    // Loop through the fields here
    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;
    }

    Because this is more related to PHP than ACF, kindly get in touch with PHP community for more support.

    I hope this helps 🙂

  • Thanks for your code! But unfortunately it produce the following XML:

    <rooms>
    1
    </rooms>
    <floor>
    2
    </floor>
    <azaza>
    <0>
    <zaza>
    556
    </zaza>
    </0>
    <1>
    <zaza>
    557
    </zaza>
    </1>
    </azaza>
    <ololo>
    <0>
    <lola>
    558
    </lola>
    </0>
    </ololo>
    <level_1>
    <0>
    <level_2>
    <0>
    <level_3>
    559
    </level_3>
    </0>
    <1>
    <level_3>
    560
    </level_3>
    </1>
    </level_2>
    </0>
    </level_1>

    instead of:

    <rooms>
    1
    </rooms>
    <floor>
    2
    </floor>
    <azaza>
    <zaza>
    556
    </zaza>
    <zaza>
    557
    </zaza>
    </azaza>
    <ololo>
    <lola>
    558
    </lola>
    </ololo>
    <level_1>
    <level_2>
    <level_3>
    559
    </level_3>
    <level_3>
    560
    </level_3>
    </level_2>
    </level_1>

    What should I do to disable unnecessary <0>, <1>, etc. tags?

  • 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 🙂

  • What if need use numeric names for subfileds?

    <level_1>
    <123>
    <777>
    559
    </777>
    <777>
    560
    </777>
    </123>
    </level_1>
  • BTW, is_numeric($k) doesn’t work. Seems it should be is_string($k) or !is_numeric($k).

  • Hi @bart-gopnik

    The numbers you saw is the index of the repeater row. It’s there to group the rows to avoid confusion while processing it (imagine you have multiple fields in a single row).

    If you want to get rid the index numbers and you want to have numbers as the subfield name, then you need another method to do that. Unfortunately, this is more related to PHP instead of ACF. Kindly get in touch with PHP community for further support.

    Also, yes, It seems I missed the exclamation point. The correct code is !is_numeric($k). I’m sorry about that.

    I hope this makes sense 🙂

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Generate XML using sub_fields recursively’ is closed to new replies.