Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess SimpleXMLElement properties without looping
    primarykey
    data
    text
    <p>I've tried both the SimpleXMLElement XPath approach as well as the DOMDocument XPath approach, but cannot seem to get things straight either way, but at least with the SimpleXMLElement route, I could actually print out the results var and see what's going on. (See Results Below)</p> <p>I want to use XPath querys without having to loop through the results. I guess I just want to access the results like an array, but the issue seems to be getting past the SimpleXMLElement objects within that results array.</p> <p>I'm querying an HTML snippet by div with a specific class. There are several divs of this particular class though, so all of them are being returned, which is fine. That's what I want. But now I want to access each and get their values, only they have child divs in most cases.</p> <p>Here's the line I'm using to execute the XPath query:</p> <pre><code>$stats = $xml-&gt;xpath("//div[contains(@class,'line-item')]"); </code></pre> <p>Here's a print_r() output of the results:</p> <pre><code>// Output of $stats Array ( [0] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [class] =&gt; line-item ) [div] =&gt; Array ( [0] =&gt; Total items shipped [1] =&gt; 50 ) ) [1] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [class] =&gt; line-item-total ) [div] =&gt; Array ( [0] =&gt; TOTAL EARNINGS * [1] =&gt; $267.23 ) ) [2] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [class] =&gt; line-item-links ) [a] =&gt; View full report ) [3] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [class] =&gt; line-item ) [div] =&gt; Array ( [0] =&gt; Ordered items [1] =&gt; 42 ) ) [4] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [class] =&gt; line-item ) [div] =&gt; Array ( [0] =&gt; Clicks [1] =&gt; 428 ) ) [5] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [class] =&gt; line-item ) [div] =&gt; Array ( [0] =&gt; Conversion [1] =&gt; 9.81% ) ) [6] =&gt; SimpleXMLElement Object ( [@attributes] =&gt; Array ( [class] =&gt; line-item-links ) [a] =&gt; View full report ) ) </code></pre> <p>Now, since I know where each value is, I thought I could just access them like so:</p> <pre><code>$y['clicks'] = "{$stats[4]['div'][0]}: {$stats[5]['div'][1]}\n"; $y['ordered'] = "{$stats[3]['div'][0]}: {$stats[4]['div'][1]}\n"; $y['shipped'] = "{$stats[0]['div'][0]}: {$stats[0]['div'][1]}\n"; $y['conversion'] = "{$stats[5]['div'][0]}: {$stats[6]['div'][1]}\n"; // Doesn't work $y['rate'] = "{$stats[1]['div'][0]}: {$stats[4]['div'][1]}\n"; // Neither encased in {} $y['total'] = 'Yesterday\'s Earnings: '.$stats[1]['div'][1]."\n"; // Nor as concatenated </code></pre> <p>The obvious problem is the SimpleXMLElement object wedged in between the items in the $stats array and the ['div'] array whose sub-items' values I'm after, which are hiding behind those SimpleXMLElement objects. </p> <p>So how would I get at those values beyond the SimpleXMLElement objects, without looping through the $stats array? Anything similar to what I've already tried above?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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