Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm working with a multi-dimensional PHP array that is converted from an XML file, and am struggling to grab specific attributes from all key names (I don't know the name of all keys, but they all have the same attributes.)

Each Key inside "$player_stats" is structured like this in the array:

[RandomKeyName] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [assists] => 0.10
                [rebounds] => 8
                [operator] => >
                [overall] => 1.45
            )

    )

I'm trying to achieve something like below. Am I not able to grab attributes from keys when using $key => $value?

foreach ($player_stats as $key => $value) {

    $rebounds = $key->rebounds;
    $assists = $key->assists;

    echo "$key has $rebounds Rebounds and $assists Assists. <br>";
}

$key works in this example but the attributes I tried grabbing don't. Any tips or pointers for grabbing specific attributes for all keys without knowing the key names would be great, thanks!

edit:

The part of the XML I'm trying to get key objects of:

<Player_Stats>
  <RandomKeyName1 assists="0.04" rebounds="9" operator="&gt;" overall="0.78" />
  <RandomKeyName2 assists="0.04" rebounds="4" operator="&gt;" overall="2.07" />
  <RandomKeyName3 assists="0.04" rebounds="1" operator="&gt;" overall="3.76" />
  <RandomKeyName4 assists="0.04" rebounds="10" operator="&gt;" overall="0.06" />
</Player_Stats>
share|improve this question
$value->rebounds ? – Patashu Mar 26 at 3:17
The keys don't actually have values in the XML file, they just have added attributes which is what I'm after. So $value is empty. foreach $key => $value is just the only way I know how to access all the keys without knowing their actual names :/ – Zack Taylor Mar 26 at 3:21
Since the PHP array is more than one level deep, maybe you are not going deep enough? – Patashu Mar 26 at 3:24
It is deep enough because when I do echo "$key <br>"; in the loop, it does list out all of the keys – Zack Taylor Mar 26 at 3:26
Any chance you can demonstrate the look of the XML? – Passerby Mar 26 at 3:28
show 1 more comment

1 Answer

up vote 0 down vote accepted

If I've understood you correctly, $value is a SimpleXMLElement object. You could get the attributes using SimpleXMLElement::attributes, which you can iterate across with another foreach.

Which would look like this (though I haven't tested this myself).

foreach ($player_stats as $xmlKey => $xmlElement) {

    foreach ($xmlElement->attributes() as $attrKey => $value) {

        if ($attrKey === 'rebounds')
            $rebounds = $value;

        if ($attrKey === 'assists')
            $assists = $value;

    }
    echo "$xmlKey has $rebounds Rebounds and $assists Assists. <br>";
}
share|improve this answer
This actually worked without any adjustments. Perfect! Now I have a much better understanding of what I should've been doing. Thanks! – Zack Taylor Mar 26 at 3:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.