Jump to content

listing multiple arrays in order


unistake
Go to solution Solved by requinix,

Recommended Posts

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

Link to comment
Share on other sites

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 by unistake
Link to comment
Share on other sites

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 by requinix
removing appid
Link to comment
Share on other sites

  • Solution

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;
 
?>
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.