unistake Posted January 25, 2016 Share Posted January 25, 2016 Hi all, I currently have an array that is shown like this when I use print_r(array_values($temperature)); Array ( [0] => SimpleXMLElement Object ( [0] => 9.29 ) [1] => SimpleXMLElement Object ( [0] => 11.37 ) [2] => SimpleXMLElement Object ( [0] => 13 ) [3] => SimpleXMLElement Object ( [0] => 14 ) [4] => SimpleXMLElement Object ( [0] => 8 ) [5] => SimpleXMLElement Object ( [0] => 13.81 ) [6] => SimpleXMLElement Object ( [0] => 19.84 ) [7] => SimpleXMLElement Object ( [0] => 22 ) ) I want to find the highest and lowest value from the above array such as here 8 is the lowest and 22 being the highest value. I am not sure how I can order the array inside an array to do this. Thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted January 25, 2016 Share Posted January 25, 2016 Turn everything into a int, like using array_map with intval, then do min() and max() on the final array. Looks like you're getting this from XML? You might be able to do the min/max from within the XML itself, depending on what the markup is... Quote Link to comment Share on other sites More sharing options...
unistake Posted January 25, 2016 Author Share Posted January 25, 2016 (edited) Yes it's from XML. I have so far, <?php $sql = "SELECT * FROM airports GROUP BY airports.IATA ORDER BY Longitude + 0 DESC"; $result = mysqli_query($cxn,$sql) or die ($cxn->error); $hottestcoldest = array(); while($row=mysqli_fetch_assoc($result)) { $xml=simplexml_load_file("$link") or die("Error: Cannot create object"); $hottestcoldest[$row['Name']] = $xml->temperature[value]; } $new = array(); foreach ($hottestcoldest as $key => $item) { $new[] = $item; } asort($new); echo reset($new).' coldest <br />'; echo end($new. ' hottest'; ?> EDIT: I guess there is a much better way to try and do it! Edited January 25, 2016 by unistake Quote Link to comment Share on other sites More sharing options...
requinix Posted January 25, 2016 Share Posted January 25, 2016 Your code doesn't make sense. Is that really what you have? Quote Link to comment Share on other sites More sharing options...
unistake Posted January 25, 2016 Author Share Posted January 25, 2016 yes! I have got confused as its not something I have attempted before and tried to mix multiple scripts found on other sites. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 26, 2016 Share Posted January 26, 2016 Alright, then we need to start from the beginning. What is the XML you have ($link?) and what does the database have to do with it? Quote Link to comment Share on other sites More sharing options...
unistake Posted January 26, 2016 Author Share Posted January 26, 2016 (edited) I am using a weather XML from openweathermap to get the hottest and coldest location according to the locations found in a php query. The php query below works fine but i know the way I am trying to sort the array values is not working. I presume because it is multiple arrays. The output I got when printing the full array content previously was: Array ( [0] => SimpleXMLElement Object ( [0] => 9.29 ) [1] => SimpleXMLElement Object ( [0] => 11.37 ) [2] => SimpleXMLElement Object ( [0] => 13 ) [3] => SimpleXMLElement Object ( [0] => 14 ) [4] => SimpleXMLElement Object ( [0] => 8 ) [5] => SimpleXMLElement Object ( [0] => 13.81 ) [6] => SimpleXMLElement Object ( [0] => 19.84 ) [7] => SimpleXMLElement Object ( [0] => 22 ) ) <?php session_start(); include_once("cxn.inc"); $today = date("Y-m-d H:i:s"); $sql = "SELECT airports.Name,airports.Country FROM airports INNER JOIN rosters ON airports.IATA = rosters.Arr WHERE rosters.Code = '$code' AND rosters.SectorDate >= '$today'"; $result = mysqli_query($cxn,$sql) or die ($cxn->error); $hottest = array(); while($row=mysqli_fetch_assoc($result)) { $link = 'http://api.openweathermap.org/data/2.5/weather?q='.$row['Name'].','.$row['Country'].'&units=metric&mode=xml&appid=*'; /* WEATHER API */ $xml=simplexml_load_file("$link") or die ("Cannot create object."); $hottest[] = $xml->temperature[value]; } asort($hottest); // attempting to sort $hottest array by numerical value order /// OUTPUT GOAL /// echo reset($hottest); // coldest value echo '<br />'; echo end($hottest); // hottest value ?> Edited January 26, 2016 by requinix removing appid Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted January 26, 2016 Solution Share Posted January 26, 2016 FYI, next time you post code, make sure you remove sensitive information like the appid you had in that URL. So that code makes more sense than the one earlier, and doesn't have as much that needs to change. You already have a loop that goes over all the locations, so do the work of determining the highest and lowest in there. Do you only care about the temperatures? You like you don't care where the temperatures are from - just what they are? <?php session_start(); include_once("cxn.inc"); $today = date("Y-m-d H:i:s"); $sql = "SELECT airports.Name,airports.Country FROM airports INNER JOIN rosters ON airports.IATA = rosters.Arr WHERE rosters.Code = '$code' AND rosters.SectorDate >= '$today'"; $result = mysqli_query($cxn,$sql) or die ($cxn->error); $coldest = $hottest = null; while($row=mysqli_fetch_assoc($result)) { $link = 'http://api.openweathermap.org/data/2.5/weather?q='.$row['Name'].','.$row['Country'].'&units=metric&mode=xml&appid=*'; /* WEATHER API */ $xml=simplexml_load_file("$link") or die ("Cannot create object."); $temp = (int)$xml->temperature['value']; // converting to int is important or you'll have a bunch of SimpleXMLElement objects if (is_null($coldest) || $temp < $coldest) { $coldest = $temp; } if (is_null($hottest) || $temp > $hottest) { $hottest = $temp; } } /// OUTPUT GOAL /// echo $coldest; echo '<br />'; echo $hottest; ?> 1 Quote Link to comment Share on other sites More sharing options...
unistake Posted January 26, 2016 Author Share Posted January 26, 2016 (edited) thanks for removing my id! sorry i just need the location and the temperature from the xml. Edit: I could just link the $row['Name'] to the temperature value though, i guess. Edited January 26, 2016 by unistake Quote Link to comment Share on other sites More sharing options...
requinix Posted January 26, 2016 Share Posted January 26, 2016 Then it's easy to change the script. Right now it tracks just the coldest and hottest temperatures; add two more variables for the coldest and hottest locations. Quote Link to comment Share on other sites More sharing options...
unistake Posted January 26, 2016 Author Share Posted January 26, 2016 works a treat! thanks!! if (is_null($coldest) || $temp < $coldest) { $coldest_loc = $row['Name']; $coldest = $temp; } if (is_null($hottest) || $temp > $hottest) { $hottest_loc = $row['Name']; $hottest = $temp; } 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.