I'm trying to get the maximum and the minimum value of the option-node for each item in the following xml (snippet):
<body>
<page number="1">
<itemset name="a">
<item id="1">
<option value="0">no</option>
<option value="1">rarely</option>
<option value="3">sometimes</option>
<option value="5">often</option>
<scale id="ho" />
</item>
<item id="2">
<option value="0">no</option>
<option value="1">rarely</option>
<option value="3">sometimes</option>
<option value="5">often</option>
<scale id="hi" />
</item>
</itemset>
</page>
</body>
I'm doing this within a foreach-loop...
...
$scid="ho";
$total=0;
$count=0;
foreach ($txml->xpath("//item[scale/@id='$scid']") as $item) {
$score=$rxml->xpath("//item[@id='".$item['id']."']"); // get a value from another simple_xml-object
$total+=intval($score[0]);
$count++;
// ******* problem starts here...
$values = $item->option['value'];
// doing sth with $values to get max and min value
echo '<pre>';
print_r($values); // *******
echo '</pre>';
} // foreach $item
...
What I get in $values
is
SimpleXMLElement Object
(
[0] => 0
)
with $values = $item->xpath('option[@value]');
instead, I get...
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[value] => 0
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[value] => 1
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[value] => 3
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[value] => 5
)
)
)
I am looking for a more straightforward solution - or how to get min and max from there.
I'd like to get sth like $values['max']=5
and $values['min']=0
Thank you in advance,
Michi
PS: as I wasn't able to get this working with simple_xml and didn't want to switch to DOM, I decided to do it quick and dirty with some basic PHP:
foreach ($item->xpath("//item[@id='".$item['id']."']/option/@value") as $value) {
$v[]=(int)$value->value;
}
echo max($v);
echo min($v);