SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 34 of 34
  1. #26
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, thank you! I thought it might be to do with the changing from HTTP to FTP...

    Just two more questions, I hope you don't mind me asking...

    1. How come the array doesn't include every column? (i.e. it has entry numbers 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 18, 20, 21 - what happened to 1, 2, 3, 4, 11, 17, 19?)

    2. Is it possible to remove leading zeros from one of the columns? (i.e. the wind speed comes up as SSW/005, could that be modified to to SSW/5?

    Thanks again, I really appreciate it!
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  2. #27
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Alexandra
    1. How come the array doesn't include every column? (i.e. it has entry numbers 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 18, 20, 21 - what happened to 1, 2, 3, 4, 11, 17, 19?)
    Well, I've gotta hand it to Gaheris for his semi-obfuscated code in the solution. Let's hear it for rarely-used functions like create_function()! The reason you're "missing" some indexes is because array_filter() only keeps values in the result array if the callback function returns true on the value, but it also happens to preserve array keys in the process. So your missing indexes were just blank chunks left over from the explode() process (i.e. the string length was <= 0).

    Quote Originally Posted by Alexandra
    2. Is it possible to remove leading zeros from one of the columns? (i.e. the wind speed comes up as SSW/005, could that be modified to to SSW/5?
    You can use number_format to handle that.

  3. #28
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again for coming to the rescue!

    I really need it to hold the value of each column (regardless of whether it holds data) to ensure the app continues to work. At the moment, whenever something becomes a 0 value, the assigned numbers change and the application fails to display the correct information.

    Eg:

    It should show Current Temperature, Wind and Conditions which it does on the first reading. However, because a reading has changed to 0, it now displays humidity, current temperature. Do you see the problem? The labels remain the same, but the data changes.

    Also, is there a reason why when there are two instances of "Pearce" it always uses the second?

    Would it be possible to use the first?
    Last edited by Alexandra; Dec 8, 2003 at 01:31.
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  4. #29
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really need it to hold the value of each column (regardless of whether it holds data) to ensure the app continues to work
    Sorry for that, array_values should redo the indexes.

    Also, is there a reason why when there are two instances of "Pearce" it always uses the second?
    Yes, as I go trough the file line by line the second entry will overwrite the first one. If you don't want that you can check for an existing entry and add the new one depending on that return value.

    Well, I've gotta hand it to Gaheris for his semi-obfuscated code in the solution.
    I have to look at it as I don't remember what I coded there.

  5. #30
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where would I put "array_values"? Should it replace "array" in the code?
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  6. #31
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At the generation of the $t variable, add array_values before the array_filter function call.
    PHP Code:

    $t 
    =    array_values(array_filter(array_map('trim'explode(' ',
            
    substr($linestrlen($match[1])))),
            
    create_function('$v''if (strlen($v) > 0) return true; else return false;'))); 

  7. #32
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! I think it will now work perfectly. Now all I have to do is familiarise myself with how this actually works :-D

    You have been a great help (along with mattjacob)! Thanks again guys!
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  8. #33
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should answer your question/s from the PM.
    PHP Code:
    <?php
    $a 
    file('ftp://ftp2.bom.gov.au/anon/gen/fwo/IDY03028.txt');

    $r = array();
    foreach (
    $a as $line) {
        if (
    preg_match('/^([A-Z][a-z]+ (?:[a-zA-Z]+)?)/'$line$match)) {
            
    $t array_values(array_filter(array_map('trim'explode(' '
                 
    substr($linestrlen($match[1])))), 
                 
    create_function('$v''if (strlen($v) > 0) return true; else return false;')));
            
    $name trim($match[1]);
            if (isset(
    $r[$name])) {
                foreach (
    $r[$name] as $i => $v) {
                    if (
    substr_count($v'-') < 
                        
    substr_count($t[$i], '-')) {
                        
    $t[$i] = $v;
                    }
                }
            }
            
    $r[$name] = $t;    
        }
    }

  9. #34
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Gaheris....That appears to have worked. You're a legend!

    :-D
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •