xml_parse_into_struct()
xml_parse_into_struct() — Parse XML data into an array structure.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
This function parses an XML string into 2 parallel array structures, one (index) containing pointers to the location of the appropriate values in the values array. These last two parameters must be passed by reference.
Signature
xml_parse_into_struct(XMLParser $parser, string $data, array &$values, array &$index = null): int|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $parser | XMLParser | A reference to the XML parser. |
| $data | string | A string containing the XML data. |
| &$values | array | An array containing the values of the XML data |
| &$index | array | An array containing pointers to the location of the appropriate values in the $values. |
Return value
xml_parse_into_struct() returns 0 for failure and 1 for success. This is not the same as false and true, be careful with operators such as ===.
Example
A runnable snippet:
$simple = "<para><note>simple note</note></para>"; $p = xml_parser_create(); xml_parse_into_struct($p, $simple, $vals, $index); echo "Index array\n"; print_r($index); echo "\nVals array\n"; print_r($vals);
Output:
Index array Array ( [PARA] => Array ( [0] => 0 [1] => 2 ) [NOTE] => Array ( [0] => 1 ) ) Vals array Array ( [0] => Array ( [tag] => PARA [type] => open [level] => 1 ) [1] => Array ( [tag] => NOTE [type] => complete [level] => 2 [value] => simple note ) [2] => Array ( [tag] => PARA [type] => close [level] => 1 ) )
Changelog
| Version | Change |
|---|---|
| 8.0.0 | parser expects an XMLParser instance now; previously, a valid xml resource was expected. |
All PHP 8.5 built-in functions
Reference card built from the PHP manual page xml_parse_into_struct() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.