简体   繁体   中英

Parsing multiple XML feeds with PHP into one sorted array

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.

I'm not quite sure what kind of sorting you're trying to do (you should specify with some examples). But optimally, you wouldn't be loading XML fragments into your array.

$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) {
 $VAR1Array[] = (int) $result['VAR1'];
 $VAR2Array[] = (int) $result['VAR2'];
 //... etc etc
}

Last, sort() works by reference, so don't equate it to a variable (ie just say sort($array); as the entire line, and then $array will be sorted. If you cast as int s like I do in the above example you can use php's default sort function without using a user-defined comparison function like others suggested. And array_push is a little bit slower and harder to read than using php's $var[] syntax to add a new element to an array.

Also, I'm fully sure that you can't assign $xmlfile that way (making unescaped double quotes inside double quotes.

In this code, the best way to define $xmlfile would be:

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

or

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

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?

$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 < $b) ? -1 : 1;
}

$sortColumn = 'VAR1'; // make sure it's a clean string

uasort($table, create_function('$a,$b', 'return cmp_row($a, $b, "'.$sortColumn.'")'));

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM