Note that there are some explanatory texts on larger screens.

plurals
  1. POsimple_xml get max value of attribute
    text
    copied!<p>I'm trying to get the maximum and the minimum value of the option-node for each item in the following xml (snippet):</p> <pre><code>&lt;body&gt; &lt;page number="1"&gt; &lt;itemset name="a"&gt; &lt;item id="1"&gt; &lt;option value="0"&gt;no&lt;/option&gt; &lt;option value="1"&gt;rarely&lt;/option&gt; &lt;option value="3"&gt;sometimes&lt;/option&gt; &lt;option value="5"&gt;often&lt;/option&gt; &lt;scale id="ho" /&gt; &lt;/item&gt; &lt;item id="2"&gt; &lt;option value="0"&gt;no&lt;/option&gt; &lt;option value="1"&gt;rarely&lt;/option&gt; &lt;option value="3"&gt;sometimes&lt;/option&gt; &lt;option value="5"&gt;often&lt;/option&gt; &lt;scale id="hi" /&gt; &lt;/item&gt; &lt;/itemset&gt; &lt;/page&gt; &lt;/body&gt; </code></pre> <p>I'm doing this within a foreach-loop...</p> <pre><code>... $scid="ho"; $total=0; $count=0; foreach ($txml-&gt;xpath("//item[scale/@id='$scid']") as $item) { $score=$rxml-&gt;xpath("//item[@id='".$item['id']."']"); // get a value from another simple_xml-object $total+=intval($score[0]); $count++; // ******* problem starts here... $values = $item-&gt;option['value']; // doing sth with $values to get max and min value echo '&lt;pre&gt;'; print_r($values); // ******* echo '&lt;/pre&gt;'; } // foreach $item ... </code></pre> <p>What I get in <code>$values</code> is</p> <pre><code>SimpleXMLElement Object ( [0] =&gt; 0 ) </code></pre> <p>with <code>$values = $item-&gt;xpath('option[@value]');</code> instead, I get...</p> <pre><code> Array ( [0] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [value] =&gt; 0 ) ) [1] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [value] =&gt; 1 ) ) [2] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [value] =&gt; 3 ) ) [3] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [value] =&gt; 5 ) ) ) </code></pre> <p>I am looking for a more straightforward solution - or how to get min and max from there.</p> <p>I'd like to get sth like <code>$values['max']=5</code> and <code>$values['min']=0</code></p> <p>Thank you in advance,</p> <p>Michi</p> <p>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:</p> <pre><code>foreach ($item-&gt;xpath("//item[@id='".$item['id']."']/option/@value") as $value) { $v[]=(int)$value-&gt;value; } echo max($v); echo min($v); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload