vote up 1 vote down
star

I want to create a sortable list that looks something like

  • $VAR1[0], $VAR2[0]...
  • $VAR1[1], $VAR2[1]...

The data comes from multiple same structured xml files:

$xmlfile="
<Level1>
 <Level2>
  <Level2Item VAR1="1" VAR2="2" ... />
  <Level2Item VAR1="4" VAR2="5" ... />
  <Level2Item VAR1="7" VAR2="8" ... />
 </Level2>
</Level1>";

//Extract each item
$xml = new SimpleXMLElement($xmlfile);
foreach ($xml->Level2[0] as $result) {
 array_push($VAR1Array, $result['VAR1']);
 array_push($VAR2Array, $result['VAR2']);
 //... etc etc
}
//sort
//$sortedArray = sort($VAR1Array);

Output

Array(
  [0] => SimpleXMLElement Object([0] => 1)
  [1] => SimpleXMLElement Object([0] => 4)
  [2] => SimpleXMLElement Object([0] => 7)
)

From this XML structure, what's the best way of storing the data in one array? I want to be able to gather all the data in one array so I can sort it by one or 2 VARs and display the results.

flag
add comment

2 Answers:

vote up 0 vote down

If I were you, I'd just push all of the SimpleXMLElements onto an array and then use uasort() with a custom callback function to sort as you desire. Does that work?

link|flag
add comment
vote up 0 vote down
$xml = simplexml_load_file(...); $table = array(); foreach ($xml->Level2[0] as $result) $table[] = $result->attributes(); function cmp_row($a, $b, $sortColumn) { if ($a == $b) return 0; return ($a

You can also keep the SimpleXMLElements and sort them directly, if you want, like apinstein said.

link|flag
comments (1)

Your Answer:

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.