…still on this I’m afraid … struggling to get some good examples - of course oddz posted an example but I got a little confused as to how I could apply it to my situation… which is (as I posted a few back):
$first = $xml->xpath("//subevent[@title='Hottest day of the year']/selection[@name]");
$second = $xml_2->xpath(".//*[@day]");
$arr = array($first, $second);
for($x=0; $x<=1; $x++) {
foreach($arr[$x] as $item){
$it1 = array($item['name'], $item['day']);
$it2 = array($item['backp1'], $item['amount']); // these are numbers but may need converting
printf('%s = %f<br />', $it1[$x], $it2[$x]);
}
}
…any similar examples or further pushes are very welcome right now <<
Thanks… I’m still struggling - let me show you what I’m trying so maybe you can see what’s going wrong. Imagine I’m getting feeds from 2 different sites and my objective is to display them in ascending order:
$first = $xml->xpath("//subevent[@title='Hottest day of the year']/selection[@name]");
$second = $xml_2->xpath(".//*[@day]");
$arr = array($first, $second);
for($x=0; $x<=1; $x++) {
foreach($arr[$x] as $item){
$it1 = array($item['name'], $item['day']);
$it2 = array($item['backp1'], $item['amount']); // these are numbers but may need converting
sort($it2); // NOT working as it should
printf('%s = %f<br />', $it1[$x], $it2[$x]);
}
}
…I know that both of the paths work and the correct information is displayed from both feeds but I am unable to sort it >> ‘$it1[0]’ returns ‘0’ when I have ‘sort($it2)’ and as it should when this is removed…
>> can anyone spot the problem or if you have a suggestion I would love to hear it <<
Does anyone know if there is a way to put the xml data into arrays and then sort it? I put it into an array but wasn’t able to sort it because I don’t think it’s coming through as numbers but (int) didn’t work in order to convert it…
There are a couple of different approaches to sorting the SimpleXML elements. One would be to literally loop over the items, pushing their values (or a subset of) into an array then sorting that array. Another would be to use SPL classes to sort the SimpleXML objects themselves (though this may be a big leap, conceptually, if you’ve not used this kind of thing before).
I think the easiest way to sort XML is to use XSL.
The short of it is to build a XSL string and use it to transform the XML as needed. Although if you didn’t need to change the sorting the XSL could actually be a separate file.
foreach($xml->xpath("//subevent[@title='Hottest day of the year']/selection[@name]") as $item) {
printf('%s = %f<br />', $item['name'], $item['backp1']);
}
July = 2.200000
August = 2.800000
November = 2.400000
December = 1.900000
That gives all months, so now we narrow the requirements by adding in attribute selectors:
“//subevent[@title=‘Hottest day of the year’]/selection[@name]”
foreach($xml->xpath("//subevent[@title='Hottest day of the year']/selection[@name]") as $item) {
printf('%s = %f<br />', $item['name'], $item['backp1']);
}
July = 2.200000
August = 2.800000
Now you can simplify by using * for the node names, and end up in this case with something close to what you started with in the first place:
“//[@title=‘Hottest day of the year’]/[@name]”
This provides the same output as before:
foreach($xml->xpath("//*[@title='Hottest day of the year']/*[@name]") as $item) {
printf('%s = %f<br />', $item['name'], $item['backp1']);
}
Looks good … not sure why but I’m getting the error message "Cannot instantiate non-existent class: simplexmlelement in … ". Can anyone suggest as to why?