3

I have this:

  [1]=>
object(SimpleXMLElement)#6 (1) {
  ["@attributes"]=>
  array(14) {
    ["name"]=>
    string(5) "MySQL"
    ["acknowledged"]=>
    string(1) "1"
    ["comments"]=>
    string(1) "1"
    ["current_check_attempt"]=>
    string(1) "1"
    ["downtime"]=>
    string(1) "0"
    ["last_check"]=>
    string(19) "2010-05-01 17:57:00"
    ["markdown_filter"]=>
    string(1) "0"
    ["max_check_attempts"]=>
    string(1) "3"
    ["output"]=>
    string(42) "CRITICAL - Socket timeout after 10 seconds"
    ["perfdata_available"]=>
    string(1) "1"
    ["service_object_id"]=>
    string(3) "580"
    ["state"]=>
    string(8) "critical"
    ["state_duration"]=>
    string(6) "759439"
    ["unhandled"]=>
    string(1) "0"
  }
}

(I used var_dump($child) to generate that)

How do I get the 'name' attribute out of there as a string?

Here is my code:

$xml = simplexml_load_string($results);

foreach($xml->data->list as $child) {
var_dump($child);
  echo $child->getName() . ": " . $child->name . "<br />";
  }
hakre
  • 193,403
  • 52
  • 435
  • 836
seanp2k
  • 53
  • 1
  • 1
  • 4

5 Answers5

13

With SimpleXML, you can get :

  • sub-elements, using object notation : $element->subElement
  • and attributes, using array notation : $element['attribute']


So, here, I'd say you'd have to use :

echo $child['name'];


As a reference, and for a couple of examples, see the Basic usage section of simplexml's manual.

Example #6 should be the interesting one, about attributes.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • I had this exact problem, and I had to add `(string)` in front of all the attributes in order to get them as strings. Very confusing. (Thanks, PHP!) Edit: I just realized that JW. said the same thing below. Anyway, hope this helps for those of you that rush to the answer marked as correct and don't look at any of the other answers. – HartleySan May 19 '16 at 16:47
13

While you can do:

echo $child['name'];

to see the value, you should note that $child['name'] is an object, not a string. Echoing it casts it to a string, so it works in that situation. But if you're storing it somewhere, it's better to cast it to a string yourself:

$name = (string) $child['name'];
JW.
  • 50,691
  • 36
  • 115
  • 143
2

Kind of messy, but I used this successfully

foreach($xml->data->children() as $child) {
//var_dump($child);
    foreach ($child->attributes() as $a => $b) {
     echo $a . '=' . $b . '<br />';
    }
}

Not sure why, but the OpsView API returns a two-dimensional array instead of just having one value per XML node :(

echo $child['name'];

works and is much more elegant, thank you.

seanp2k
  • 53
  • 1
  • 1
  • 4
1

This was really confusing when i first encountered this. While trying to get the whole array, instead I was seemingly returned the first object in the array instead! In my instance "item" was an array of objects and the following only returned the first in the array!

$response->channel->item

However if like me, you want to access all the objects you can just loop over item, so not really a problem. Just really strange!

foreach($response->channel->item as $item) {
    ray($item);
}
James Baird
  • 97
  • 1
  • 9
0

I had a similar problem, I needed to get the string out of my SimpleXMLElement, I couldn't find the name to call it. Found the solution, by using (string) to get the string text:

foreach ($lines as $line) {
    array_push($result, new line(**(string)**$line));
}

array
  0 => 
    object(line)[190]
      private '_line' => 
        object(SimpleXMLElement)[128]
          public '@attributes' => 
            array
              ...
          string ' ' (length=1)
  1 => 
    object(line)[191]
      private '_line' => 
        object(SimpleXMLElement)[131]
          public '@attributes' => 
            array
              ...
          string ' ' (length=1)
  2 => 
    object(line)[192]
      private '_line' => 
        object(SimpleXMLElement)[132]
          public '@attributes' => 
            array
              ...
          string ' ~54**** I N V O I C E ****' (length=27)
CE_REAL
  • 384
  • 5
  • 13