PHP xml_parse_into_struct() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
XML Parser

Definition

In PHP, xml_parse_into_struct() parses an XML string and fills two arrays for you, so you do not need to write any handlers.

The first array lists every tag in order with its type, level, attributes, and text. The optional second array tells you at which positions each tag name appears. It is a quick option for small XML strings.

Syntax

Call xml_parse_into_struct() with the values you need. Start with this line.

PHP
$result = xml_parse_into_struct($parser, $xml, $values, $index);
CallWhat it does
xml_parse_into_struct($parser, $xml, $values, $index)Fills $values with all tags and $index with the positions of each tag name.
xml_parse_into_struct($parser, $xml, $values)Fills only $values. The $index argument is optional.
$values[1]['value']Reads the text of the tag stored at position 1.

This function replaces any handlers you set earlier, because it uses its own. It returns 1 on success and 0 on failure. Tag names are upper case unless you turn case folding off.

Library declarations
PHP
xml_parse_into_struct(XMLParser $parser, string $data, array &$values, array &$index = null): int

Parameters

ParameterDescription
parserRequired. The XMLParser object created by xml_parser_create() or xml_parser_create_ns().
dataRequired. The XML string to parse.
valuesRequired. A variable passed by reference. It receives an array with one entry per tag.
indexOptional. A variable passed by reference. It receives an array that maps each tag name to its positions in $values.

Return Value

Return ValueDescription
int1 on success, 0 on failure.

PHP Examples

Example 1 — Turn XML into Arrays

Parse a tiny XML string and print the tags, then look at the index array.

PHP
<?php
$xml = '<root><item id="1">Tea</item></root>';

$parser = xml_parser_create();
$result = xml_parse_into_struct($parser, $xml, $values, $index);

echo "Result: ", $result, PHP_EOL;

foreach ($values as $position => $node) {
    echo $position, ": ", $node['tag'], " (", $node['type'], ", level ", $node['level'], ")", PHP_EOL;
}

print_r($index);

How It Works

  1. xml_parser_create() makes the parser. No handlers are needed here.
  2. xml_parse_into_struct() fills $values and $index through references.
  3. ROOT appears twice, as an open tag and as a close tag, while ITEM is complete because it has only text.
  4. The index array lists the positions of each tag name in $values.

Example 2 — Find Repeated Tags with $index

Use the index array to see where a repeated tag name appears in the values.

PHP
<?php
$xml = '<list><item>Tea</item><item>Milk</item></list>';

$parser = xml_parser_create();
xml_parse_into_struct($parser, $xml, $values, $index);

foreach ($index as $tag => $positions) {
    echo $tag, ": ", implode(', ', $positions), PHP_EOL;
}

How It Works

  1. The XML has one list tag with two item tags inside.
  2. $values holds LIST open, ITEM, ITEM, and LIST close at positions 0, 1, 2, and 3.
  3. $index groups those positions by tag name.
  4. implode() joins the numbers so each tag prints on a single line.

Example 3 — Read Attributes and Text

Turn case folding off so attribute names stay as written, then read each item's id and text.

PHP
<?php
$xml = '<root><item id="1">Tea</item><item id="2">Milk</item></root>';

$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser, $xml, $values);

foreach ($values as $node) {
    if ($node['type'] === 'complete') {
        echo $node['attributes']['id'], "=", $node['value'], PHP_EOL;
    }
}

How It Works

  1. xml_parser_set_option() turns off case folding, so keys such as id stay in lower case.
  2. The call passes only three arguments, because $index is optional.
  3. Only entries with the type complete are used, since they hold both attributes and text.
  4. Each line prints the id attribute and the text of an item.

Frequently Asked Questions

It parses an XML string and stores the result in arrays. $values lists each tag in order, and $index maps tag names to their positions in $values.
No. The function sets its own handlers internally. Any handlers you set earlier are replaced.
An open entry is a start tag that has child tags, a close entry is its end tag, and a complete entry is a tag that holds only text or is empty.
The parser folds case by default. Call xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0) before parsing to keep the original case.
No. The full document is stored in memory as arrays. For large files, use xml_parse() with handlers and process the data piece by piece.

Next: xml_parser_create()

xml_parser_create() creates a new XML parser and returns it as an XMLParser object.

xml_parser_create() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful