1

I have the following XML:

<category-links>
    <category-link id="2350">
        <name locale="de">Wasserkocher</name>
    </category-link>
</category-links>

I have turned it into an array with SimpleXML, and the result looks like this:

[category-links] => SimpleXMLElement Object
    (
        [category-link] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [id] => 2350
                    )

                [name] => Wasserkocher
            )

    )

I want to output the following:

<tr>
    <td>category-link > id</td>
    <td><?= $p->{'category-links'}->{'category-link'}->{'@attributes'}->id ?></td>
</tr>
<tr>
    <td>category-link > name</td>
    <td><?= $p->{'category-links'}->{'category-link'}->name ?></td>
</tr>

Only name gives the expected result (Wasserkocher), while nothing is shown for id.

var_dump($p->{'category-links'}->{'category-link'}->{'@attributes'}->id)

gives NULL.

Replacing arrows by brackets in various ways doesn't change anything.

I guess, I could "solve" the problem with a string replace, removing the @ sign in the array.

Is there any way to output ID without making this rather clumsy measure?

Kevin
  • 41,694
  • 12
  • 53
  • 70
Watchduck
  • 1,076
  • 1
  • 9
  • 29

1 Answers1

1

No, the way of accessing the attributes is to use ->attributes() method:

<td><?= $p->{'category-links'}->{'category-link'}->attributes()->id; ?></td>

Sample Output

Kevin
  • 41,694
  • 12
  • 53
  • 70