format Posted November 26, 2013 Share Posted November 26, 2013 Hi Guys, I have an xml document wich I can view as xml or put into an array. I need to parse the xml data to find the minimum SLevel value, displaying on the page that value itself and the company offering it. I have tried in vain to get it working. I can find data by looping through matching a company name or exact value. foreach ($xml->Type->Company as $item){ if ($item->SLevel == 18.56) { do here } But ,I have no idea how to loop through using min to find the lowest number. Would anyone have a working example I could follow. Many thanks. Jon <Quotes><Type><Desc>Life Cover Only</Desc><CriticalWarning>For Friends First, conversion applies to life and Specified Illness to age 65.</CriticalWarning><Company><Name key="1">Caledonian Life</Name><PricePledge>True</PricePledge><Underwriting1/><SLevel key="2">23.98</SLevel><SConvertible key="2">25.51</SConvertible><SMortgage key="2">18.66</SMortgage><SConvMortgage key="0">-</SConvMortgage></Company><Company><Name key="2">Canada Life</Name><PricePledge>False</PricePledge><Underwriting1/><SLevel key="0">-</SLevel><SConvertible key="0">-</SConvertible><SMortgage key="0">-</SMortgage><SConvMortgage key="0">-</SConvMortgage></Company> </Type></Quotes> Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/ Share on other sites More sharing options...
Ch0cu3r Posted November 26, 2013 Share Posted November 26, 2013 You'd loop through the xml storing the SLevel into an array. Then you'd use min $SLevels[] = array(); foreach ($xml->Type->Company as $item) { $SLevels[] = $item->SLevel; // add SLevel to array } $minSLevel = min($SLevels); // get minimum value echo 'The minimum SLevel value is ' . $minSLevel; Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1460138 Share on other sites More sharing options...
format Posted November 26, 2013 Author Share Posted November 26, 2013 Thank you for such a fast reply. I tried the above on the xml pasted below and it returned the highest number which is also the last in the xml doc. Not sure why. Thanks again for your help. <Quotes><Type><Desc>Life Cover Only</Desc><CriticalWarning>For Friends First, conversion applies to life and Specified Illness to age 65.</CriticalWarning><Company><Name key="1">Caledonian Life</Name><PricePledge>True</PricePledge><Underwriting1/><SLevel key="2">23.98</SLevel><SConvertible key="2">25.51</SConvertible><SMortgage key="2">18.66</SMortgage><SConvMortgage key="0">-</SConvMortgage></Company><Company><Name key="2">Canada Life</Name><PricePledge>False</PricePledge><Underwriting1/><SLevel key="0">-</SLevel><SConvertible key="0">-</SConvertible><SMortgage key="0">-</SMortgage><SConvMortgage key="0">-</SConvMortgage></Company><Company><Name key="3">Zurich Life</Name><PricePledge>False</PricePledge><Underwriting1/><SLevel key="2">23.86</SLevel><SConvertible key="2">24.93</SConvertible><SMortgage key="2">18.56</SMortgage><SConvMortgage key="0">-</SConvMortgage></Company><Company><Name key="4">Friends First</Name><PricePledge>True</PricePledge><Underwriting1/><SLevel key="2">26.21</SLevel><SConvertible key="2">28.57</SConvertible><SMortgage key="2">20.60</SMortgage><SConvMortgage key="0">-</SConvMortgage></Company><Company><Name key="5">Aviva</Name><PricePledge>False</PricePledge><Underwriting1/><SLevel key="2">26.05</SLevel><SConvertible key="2">28.30</SConvertible><SMortgage key="2">21.27</SMortgage><SConvMortgage key="0">-</SConvMortgage></Company><Company><Name key="6">Irish Life</Name><PricePledge>True</PricePledge><Underwriting1/><SLevel key="2">24.38</SLevel><SConvertible key="2">26.17</SConvertible><SMortgage key="2">19.15</SMortgage><SConvMortgage key="2">20.54</SConvMortgage></Company><Company><Name key="7">New Ireland</Name><PricePledge>True</PricePledge><Underwriting1/><SLevel key="2">27.19</SLevel><SConvertible key="2">28.83</SConvertible><SMortgage key="2">21.34</SMortgage><SConvMortgage key="2">23.48</SConvMortgage></Company></Type></Quotes><hr /><Quotes><Type><Desc>Life Cover Only</Desc><CriticalWarning>For Friends First, conversion applies to life and Specified Illness to age 65.</CriticalWarning><Company><Name key="1">Caledonian Life</Name><PricePledge>True</PricePledge><Underwriting1/><SLevel key="2">23.98</SLevel><SConvertible key="2">25.51</SConvertible><SMortgage key="2">18.66</SMortgage><SConvMortgage key="0">-</SConvMortgage></Company><Company><Name key="2">Canada Life</Name><PricePledge>False</PricePledge><Underwriting1/><SLevel key="0">-</SLevel><SConvertible key="0">-</SConvertible><SMortgage key="0">-</SMortgage><SConvMortgage key="0">-</SConvMortgage></Company><Company><Name key="3">Zurich Life</Name><PricePledge>False</PricePledge><Underwriting1/><SLevel key="2">23.86</SLevel><SConvertible key="2">24.93</SConvertible><SMortgage key="2">18.56</SMortgage><SConvMortgage key="0">-</SConvMortgage></Company><Company><Name key="4">Friends First</Name><PricePledge>True</PricePledge><Underwriting1/><SLevel key="2">26.21</SLevel><SConvertible key="2">28.57</SConvertible><SMortgage key="2">20.60</SMortgage><SConvMortgage key="0">-</SConvMortgage></Company><Company><Name key="5">Aviva</Name><PricePledge>False</PricePledge><Underwriting1/><SLevel key="2">26.05</SLevel><SConvertible key="2">28.30</SConvertible><SMortgage key="2">21.27</SMortgage><SConvMortgage key="0">-</SConvMortgage></Company><Company><Name key="6">Irish Life</Name><PricePledge>True</PricePledge><Underwriting1/><SLevel key="2">24.38</SLevel><SConvertible key="2">26.17</SConvertible><SMortgage key="2">19.15</SMortgage><SConvMortgage key="2">20.54</SConvMortgage></Company><Company><Name key="7">New Ireland</Name><PricePledge>True</PricePledge><Underwriting1/><SLevel key="2">27.19</SLevel><SConvertible key="2">28.83</SConvertible><SMortgage key="2">21.34</SMortgage><SConvMortgage key="2">23.48</SConvMortgage></Company></Type></Quotes> Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1460143 Share on other sites More sharing options...
Ch0cu3r Posted November 26, 2013 Share Posted November 26, 2013 My bad, change $SLevels[] = array(); to $SLevels = array(); Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1460146 Share on other sites More sharing options...
Ch0cu3r Posted November 26, 2013 Share Posted November 26, 2013 (edited) Ok I tested the code. I found a few issues with it. try this instead $SLevels = array(); foreach ($xml->Type->Company as $item) { $tmp = (array) $item->SLevel; if($tmp[0] != '-') // ignore SLevels set to a dash $SLevels[] = $tmp[0]; // add SLevel to array } $minSLevel = min($SLevels); // get minimum value echo 'The minimum SLevel value is ' . $minSLevel; Edited November 26, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1460147 Share on other sites More sharing options...
format Posted November 26, 2013 Author Share Posted November 26, 2013 Thats fantastic, works brillant. Is it possible to catch the company name as well as the min value. I dont know what the rules are in the forums but the above code is for a client of mine. I am more than happy to pay someone like yourself for this kind of support. apologies if this is not your thing or not allowed here but to have someone who can help with these kind of coding issues that are above me is worth its wieght in gold. Thank you so much again. J Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1460149 Share on other sites More sharing options...
Ch0cu3r Posted November 26, 2013 Share Posted November 26, 2013 Is it possible to catch the company name as well as the min value. Yes. $SLevels = array(); foreach ($xml->Type->Company as $item) { $companyName = (string) $item->Name; // get the company name $tmp = (array) $item->SLevel; if($tmp[0] != '-') $SLevels[] = array($tmp[0], $companyName); // add SLevel and company name to array } $minSLevel = min($SLevels); // get minimum value echo $minSLevel[1] .' has the minimum SLevel value of ' . $minSLevel[0]; Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1460158 Share on other sites More sharing options...
Barand Posted November 26, 2013 Share Posted November 26, 2013 alternative using xpath search $min = 99999.99; foreach($xml->xpath('//Company') as $co) { if ($co->SLevel != '-') { $min = min($min, $co->SLevel); } } foreach( $xml->xpath("//Company[SLevel='$min']") as $lowest) { echo "$lowest->Name : $lowest->SLevel<br>"; } results Caledonian Life : 23.98Caledonian Life : 23.98 Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1460163 Share on other sites More sharing options...
format Posted November 26, 2013 Author Share Posted November 26, 2013 Thanks ch0cu3r & Barand, Your help has saved me hours.... Given the quality of responses it would be tempying to dump all my daily queries here. However I won't abuse peoples time. Thanks a million for the help on this one. Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1460175 Share on other sites More sharing options...
format Posted December 17, 2013 Author Share Posted December 17, 2013 Hi Ch0cu3r, one small thing, if SLevel in the XML doc contains a large number with a comma it seems to not like it. 1,000.01 causes 1.00 to be returned wheras 900.01 is no problem. Does something need to change in the code.? Many thanks Jon Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1462550 Share on other sites More sharing options...
Barand Posted December 17, 2013 Share Posted December 17, 2013 When converting a string to a number PHP will only consider those characters up to the the first non-numeric character, which in that case is the comma. (The decimal period is taken to be a numeric character when converting). For this reason, and others, numbers should always be stored unformatted in data and not as formatted strings. Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1462551 Share on other sites More sharing options...
format Posted December 17, 2013 Author Share Posted December 17, 2013 Thanks for this. My problem is here so, the below works until the number in minSLevel contains a comma. This is where more PHP knowledge is required. if (number_format($minSLevel[0],2) >= 11.20) {$premium = number_format($minSLevel[0] - ($minSLevel[0] * .1),2, '.', ',');}else { $premium = number_format($minSLevel[0],2, '.', ',');} Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1462552 Share on other sites More sharing options...
Barand Posted December 17, 2013 Share Posted December 17, 2013 Why are you using number_format() on strings that are already formatted? Why are you processing formatted numbers at all? You should only format numbers on final output. Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1462554 Share on other sites More sharing options...
format Posted December 17, 2013 Author Share Posted December 17, 2013 if ($minSLevel[0] >= 11.20) The above does not work when $minSLevel is 1,862.08 Only works for hundreds. Sorry, php number knowledge not great. Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1462556 Share on other sites More sharing options...
Barand Posted December 17, 2013 Share Posted December 17, 2013 Where did the 1,862.08 get formatted with the comma in the first place? You need to go back to change the earlier processing so it stays as 1862.08 until you output the results to the screen or the printed page (ie only when it's going to be seen by human eyes). Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1462561 Share on other sites More sharing options...
format Posted December 17, 2013 Author Share Posted December 17, 2013 From the xml doc returned to via a post from a 3rd party Example below DLevel key="2">1,391.52</DLevel><DConvertible key="2">1,504.23</DConvertible><JLevel key="2">1,376.31</JLevel><JConvertible key="2">1,487.68</JConvertible><JMortgage key="2">856.76</JMortgage> Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1462562 Share on other sites More sharing options...
Ch0cu3r Posted December 17, 2013 Share Posted December 17, 2013 You'll need to strip the number formatting when finding the minimum value. A modification of Barands code $min = 99999.99; foreach($xml->xpath('//Company') as $co) { if ($co->SLevel != '-') { $min = min($min, str_replace(',', '', $co->SLevel)); // strip number formatting } } $min = number_format($min, 2, ',', '.'); // format minimum value foreach( $xml->xpath("//Company[SLevel='$min']") as $lowest) { echo "$lowest->Name : $lowest->SLevel<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1462566 Share on other sites More sharing options...
format Posted December 17, 2013 Author Share Posted December 17, 2013 Thank you for your help, I added $minSLevel[0] = str_replace(',', '', $minSLevel[0]); This seems to be working but the above is far neater. I will have a go with it. Thank you very much for your time and help Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1462567 Share on other sites More sharing options...
Barand Posted December 17, 2013 Share Posted December 17, 2013 $min = min($min, str_replace(',', '', $co->SLevel)); // strip number formatting Unfortunately, if you strip out the comma at that point, the last part of the code does not find any matched as 1,234.56 != 1234.56. However you should strip out the commas at the earliest opportunity as numbers do not behave correctly as strings EG echo min('1,234.56', '23.45'); //--> 1,234.56 ??? echo min(1234.56, 23.45); //--> 23.45 Quote Link to comment https://forums.phpfreaks.com/topic/284285-find-a-minumum-value-from-an-xml-doc/#findComment-1462569 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.