How can SimpleXMLElement objects be effectively utilized in PHP for parsing XML data with duplicate field names?

When parsing XML data with duplicate field names using SimpleXMLElement objects in PHP, the issue arises because the object only retains the last occurrence of a field with the same name. To effectively utilize SimpleXMLElement objects in this scenario, you can access the data using array notation to retrieve all occurrences of the field with the same name.

$xml = '<data>
            <item>
                <name>Item 1</name>
                <price>10</price>
            </item>
            <item>
                <name>Item 2</name>
                <price>20</price>
            </item>
            <item>
                <name>Item 3</name>
                <price>30</price>
            </item>
        </data>';

$xmlObj = new SimpleXMLElement($xml);

foreach($xmlObj->item as $item){
    echo "Name: " . $item->name . ", Price: " . $item->price . PHP_EOL;
}