phporcaffeine Posted December 14, 2008 Share Posted December 14, 2008 I have an XML file that has nodes in it like this: <net>-1.0112</net> <net>1.54</net> <net>-0.0112</net> ... etc Now I am parsing them into a stack (which works fine) .... what I want to do is sort that stack, highest to lowest. The issue I have is that php doesn't seem to sort the numbers correctly. it sees -1.0112 as a higher number than 1.54 ... how do I do this? Thanks in advance Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 14, 2008 Share Posted December 14, 2008 PHP is seeing them as strings. Convert them to floats using float() during the extraction process. http://us2.php.net/floatval Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted December 14, 2008 Author Share Posted December 14, 2008 Okay, I am using the floatval() and putting the result as the key in the stack. Then I sort the stack with sort() and I get close but no cigar: <net>-0.04</net> <net>-0.05</net> <net>-0.05</net> <net>-0.05</net> <net>-0.05</net> <net>-0.06</net> <net>1.54</net> <net>-0.05</net> <net>-0.06</net> <net>-0.07</net> <net>-0.07</net> <net>-0.07</net> <net>-0.08</net> <net>-0.0905</net> <net>-0.09</net> <net>-0.09</net> <net>-0.09</net> <net>-0.11</net> <net>-0.11</net> <net>1.02</net> <net>-0.15</net> <net>-0.17</net> <net>-0.17</net> <net>-0.21</net> The goal is to sort the stack highest to lowest using the key to sort with. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2008 Share Posted December 14, 2008 To get the quickest answer as to why your code is not working, you need to post your code. 101 different people could be posting the same symptoms as you are and each one of them could be doing something different in their code that is causing that symptom. Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted December 14, 2008 Author Share Posted December 14, 2008 Here is the code: <?php class xmlreaders { var $_data; var $_white; var $_xml_url; function xmlreaders ($xml_url = "") { $this->_white = 1; if (trim($xml_url) != "") $this->set_xml_url ($xml_url); } function set_xml_url ($url) { $this->_xml_url = $url; } function read () { if (!$this->_xml_url) $this->error ("XML File is not assigned."); $fp = fopen ($this->_xml_url, "r"); while (!feof ($fp)) $this->_data .= fgets($fp, 4096); fclose ($fp); $this->_data = trim ($this->_data); } function parse () { $this->read(); if (trim ($this->_data) == "") $this->error ("Data not ready."); $vals = $index = $array = array(); $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $this->_white); xml_parse_into_struct($parser, $this->_data, $vals, $index); xml_parser_free($parser); $i = 0; $tagname = $vals[$i]['tag']; if ( isset ($vals[$i]['attributes'])) { $array[$tagname]['@'] = $vals[$i]['attributes']; } else { $array[$tagname]['@'] = array(); } $array[$tagname]["#"] = $this->xml_depth($vals, $i); return $array; } function xml_depth($vals, &$i) { $children = array(); if ( isset($vals[$i]['value'])) { array_push($children, $vals[$i]['value']); } while (++$i < count($vals)) { switch ($vals[$i]['type']) { case 'open': if ( isset ( $vals[$i]['tag'])) { $tagname = $vals[$i]['tag']; } else { $tagname = ''; } if ( isset ( $children[$tagname])) { $size = sizeof($children[$tagname]); } else { $size = 0; } if ( isset ( $vals[$i]['attributes'])) { $children[$tagname][$size]['@'] = $vals[$i]["attributes"]; } $children[$tagname][$size]['#'] = $this->xml_depth($vals, $i); break; case 'cdata': array_push($children, $vals[$i]['value']); break; case 'complete': $tagname = $vals[$i]['tag']; if( isset ($children[$tagname])) { $size = sizeof($children[$tagname]); } else { $size = 0; } if( isset ( $vals[$i]['value'])) { $children[$tagname][$size]["#"] = $vals[$i]['value']; } else { $children[$tagname][$size]["#"] = ''; } if ( isset ($vals[$i]['attributes'])) { $children[$tagname][$size]['@'] = $vals[$i]['attributes']; } break; case 'close': return $children; break; } } return $children; } function traverse_xmlize($array, $arrName = "array", $level = 0) { foreach($array as $key=>$val) { if ( is_array($val)) { traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1); } else { $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n"; } } return 1; } function error ($str) { print get_class ($this)." ".$this->version()." => $str"; exit(); } function version () { return "1.0"; } } $xml = new XMLReader(); $xmlurl = "stocksvol.xml"; $xmlreaders = new xmlreaders ($xmlurl); $xml = $xmlreaders->parse(); $new = array(); foreach ($xml['TTI_transmission']['#']['q'] as $value) { //FORMULA = $value['#']['s'][0]['#'] (WHERE 'S' IS THE NODE THAT YOU ARE TRYING TO GET THE VALUE OF $net = $value['#']['l'][0]['#'] - $value['#']['o'][0]['#']; echo $value['#']['l'][0]['#'] . "-" . $value['#']['o'][0]['#'] . "=" . $net . "<br />"; $num = floatval($net); $new[] = array($num => "<net>$net</net><q><s>" . $value['#']['s'][0]['#'] . "</s><l>" . $value['#']['l'][0]['#'] . "</l><c>" . $value['#']['c'][0]['#'] . "</c><p>" . $value['#']['p'][0]['#'] . "</p><h>" . $value['#']['h'][0]['#'] . "</h><o>" . $value['#']['o'][0]['#'] . "</o><h>" . $value['#']['h'][0]['#'] . "</h><t>" . $value['#']['t'][0]['#'] . "</t><d>" . $value['#']['d'][0]['#'] . "</d><t>" . $value['#']['t'][0]['#'] . "</t></q>"); } $date =$xml['TTI_transmission']['#']['datestamp'][0]['#']; unset($xml); unset($xmlreaders); sort($new); $cnt =0; $export = " <?xml version=\"1.0\" ?> <!DOCTYPE TTI_transmission [ <!ELEMENT TTI_transmission (q*,datestamp)> <!ELEMENT datestamp (#PCDATA)> <!ELEMENT q (s?,l?,c?,p?,v?,o?,h?,w?,d?,t?)> <!ELEMENT s (#PCDATA)> <!ELEMENT l (#PCDATA)> <!ELEMENT c (#PCDATA)> <!ELEMENT p (#PCDATA)> <!ELEMENT v (#PCDATA)> <!ELEMENT o (#PCDATA)> <!ELEMENT h (#PCDATA)> <!ELEMENT w (#PCDATA)> <!ELEMENT d (#PCDATA)> <!ELEMENT t (#PCDATA)> ]> <TTI_transmission> "; foreach ($new as $key => $value) { foreach ($value as $val) { $cnt++; if ($cnt >= 100) { break; } $export .= $val . "\n"; } } $export .= "<datestamp>" . $date . "</datestamp> </TTI_transmission>"; fwrite(fopen('export.xml', 'w+'), $export); ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2008 Share Posted December 14, 2008 I'll assume you are referring to these lines of code - $num = floatval($net); $new[] = array($num => "<net>$net</net><q><s>" . $value['#']['s'][0]['#'] . "</s><l>" . $value['#']['l'][0]['#'] . "</l><c>" . $value['#']['c'][0]['#'] . "</c><p>" . $value['#']['p'][0]['#'] . "</p><h>" . $value['#']['h'][0]['#'] . "</h><o>" . $value['#']['o'][0]['#'] . "</o><h>" . $value['#']['h'][0]['#'] . "</h><t>" . $value['#']['t'][0]['#'] . "</t><d>" . $value['#']['d'][0]['#'] . "</d><t>" . $value['#']['t'][0]['#'] . "</t></q>"); ... sort($new); You are setting the array index to $num. sort() is sorting the values (not the index) and it in fact replaces the indexes - Note: This function assigns new keys to the elements in array . It will remove any existing keys that may have been assigned, rather than just reordering the keys. You should probably look at krsort() - krsort Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.