Domanda

I have a PHP object containing an XML string that looks as follows. How can I echo the 2nd, 3rd, 4th, 5th, 6th value out of this using PHP ?

My XML:

<ranks>
  <groupCount>0</groupCount>
  <groupCount>5</groupCount>
  <groupCount>8</groupCount>
  <groupCount>14</groupCount>
  <groupCount>15</groupCount>
  <groupCount>15</groupCount>
</ranks>

If I use <?php echo $objMyObject->groupCount; ?> then this returns the first value correct so I just need a way for the others.

Many thanks for any help, Mike.

È stato utile?

Soluzione

Assuming you're using SimpleXML, you can just access it as though there was a numeric array:

// The first item is this...
echo $objMyObject->groupCount;
// ... but also this
echo $objMyObject->groupCount[0];
// The 6th item would be this
echo $objMyObject->groupCount[5];

// Or you can loop over them all like this
foreach ( $objMyObject->groupCount as $i => $count ) {
     echo 'The ', $i+1, 'th item is ', $count;
}

See more examples in the PHP manual.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top