Home  >  Article  >  Backend Development  >  How to Access XML Nodes with Hyphens in PHP?

How to Access XML Nodes with Hyphens in PHP?

Barbara Streisand
Barbara StreisandOriginal
2024-10-26 11:53:03879browse

How to Access XML Nodes with Hyphens in PHP?

XML Node Names with Hyphens in PHP

When attempting to extract data from XML using node names containing hyphens, a "Warning: Invalid argument supplied for foreach()" error may arise. This occurs due to PHP's naming convention restrictions.

To address this, the PHP manual recommends encapsulating such element names within braces and apostrophes. For instance, in your case, instead of:

foreach ($xml->custom-field-value as $milestone)
{
    ...     
}

You should write:

$xml->{'custom-field-value'}

This will allow you to access and iterate over elements with hyphenated node names seamlessly.

The above is the detailed content of How to Access XML Nodes with Hyphens in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn