www.webdeveloper.com
Results 1 to 6 of 6

Thread: Parsing/scanning a php array

  1. #1
    Join Date
    Jul 2008
    Posts
    92

    resolved Parsing/scanning a php array

    Say I have an array with the format (created from a few loops after a sql query)

    $array~>
    0=> [0=>[year] 1=>[month] 2=>[value]]
    1=> [0=>[year] 1=>[month] 2=>[value]]
    ... =>

    and I also have two variables, $startmonth & $endmonth.

    I want to achieve the following:
    For each year (and the years are different for each id)
    {
    if between start & end month
    {
    sum totals
    save total to a new array with only the year and it's total sum for selected months
    }
    }

    Is this possible?

    I'm stuck on scanning through the years (located at [0]). The years will range from 1920 to now. I know the logic, just not the commands. Any help?

  2. #2
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    PHP Code:
    $startmonth '052003'// 5th month of 2003
    $endmonth '072008';
    $dates = Array(
      Array(
    200353),
      Array(
    200363),
      Array(
    200873),
      Array(
    200883),
    );

    foreach(
    $dates as $date){
      
    $curDate $date[1].$date[0];
      if(
    strlen($curDate) != 6)
        
    $curDate '0'.$curDate;
      if(
    $curDate $startmonth && $curDate $endmoneth){// excluding
    //if($curDate >= $startmonth && $curDate <= $endmoneth){// including
        // not sure what you want here, but this is a valid date.
        // explain what things your summing etc and I'll help you out some more.
      
    };
    }; 
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  3. #3
    Join Date
    Jul 2008
    Posts
    92
    I'll try to explain a little clearer.

    As I said, I have two variables representing a starting month and an ending month. This should encompass a time period for the observation of values, of which I'd like to sum.

    Say a start month of 6 and an end month of 9.

    I then want to go through the array generated from an sqldb and sum up the values for month 6, 7, 8, and 9 for EACH year.

    Right now I have an element for each month with the related year and value. IE: [2007][12][4.5] which is the year.month.value.

    My first thought led me to think it'd be easier to first create an array for each year that held an array inside with a month => value relationship.

    Ie 2006 2007
    01 => 1's val 01 => 1's val
    02 => 2's val 02 => 2's val

    etc.

    Then, I could go through each year and pick out the months I need and sum them up. From here I figure I can generate a final array that I can use to create a table and display a list of the highest values of the sum of the selected months.

    My end goal is to have an array in this format:


    For months (startmonth - endmonth):
    (historic values)
    2007 => Val
    2006 => Val
    2005 => Val

    Which I can then compare against the forecasted value and generate a table of the current years value relative to historic years.

    IE,


    ... (slightly higher neighbor values) ...
    2008 Forecast Val
    ... (slightly lower neighbor values)...

    I don't really need help with anything beyond the grouping of months per each year into a single array, rather than as 12 separate elements per month.
    Last edited by mith36; 07-30-2008 at 02:42 PM.

  4. #4
    Join Date
    Jul 2008
    Posts
    92
    I'm getting close with this code
    Code:
    for ($year=$oldyr; $year <= $newyr; $year++)
    {
     for ($key= 0 ; $key <= $numrecs ; $key++)
     {      
           $next = substr($curDate[$key],2);
           if ($year == $next)
            {
                    $first = substr($curDate[$key],0,-4);
                    $second = substr($curDate[$key],0,-4);
                    if ($first >= $bw && $second <= $ew)                                          
                   {
                          $yeartot[$year] += $obsval[$key];
                   }
            }
      }
    }
    But ATM my array looks like this:
    Array ( [1997] => -39996 [1998] => -39996 [1999] => -39996 [2000] => -39996 [2001] => -39996 [2002] => -39996 [2003] => -39996 [2004] => -39996 [2005] => -39996 [2006] => -39996 [2007] => -39996 [2008] => -39996 )

    Actually, it looks like it's working, but I just need to find a way to deal with nulls (set to -9999) that are mucking things up. A different source generated this output:

    Array ( [1927] => -39996 [1928] => 266.4 [1929] => 237.4 [1930] => 170.8 [1931] => 137.5 [1932] => 206.9 [1933] => 218.5 [1934] => 88.2 [1935] => 180.1 [1936] => 247.9 [1937] => 160.4 [1938] => 253.4 [1939] => 167.4 [1940] => 146.1 [1941] => 176.3 [1942] => 186.8 [1943] => 179.5 [1944] => 160.5 [1945] => 182.8 [1946] => 155.183 [1947] => 267.59 [1948] => 166.805 [1949] => 263.715 [1950] => 152.058 [1951] => 242.239 [1952] => 290.842 [1953] => 181.261 [1954] => 116.862 [1955] => 159.504 [1956] => 212.184 [1957] => 330.793 [1958] => 216.986 [1959] => 186.223 [1960] => 226.098 [1961] => 176.445 [1962] => 286.236 [1963] => 145.736 [1964] => 176.881 [1965] => 271.068 [1966] => 117.88 [1967] => 201.123 [1968] => 186.844 [1969] => 240.447 [1970] => 237.645 [1971] => 250.133 [1972] => 182.442 [1973] => 250.153 [1974] => 241.568 [1975] => 213.396 [1976] => 146.748 [1977] => 117.569 [1978] => 289.356 [1979] => 248.642 [1980] => 217.198 [1981] => 121.533 [1982] => 197.515 [1983] => 325.591 [1984] => 343.258 [1985] => 212.977 [1986] => 313.131 [1987] => 137.736 [1988] => 217.978 [1989] => 150.585 [1990] => 199.916 [1991] => 202.495 [1992] => 184.92 [1993] => 268.204 [1994] => 186.669 [1995] => 302.845 [1996] => 274.792 [1997] => 312.677 [1998] => 196.573 [1999] => 223.963 [2000] => 218.793 [2001] => 155.449518 [2002] => 88.228797 [2003] => 264.7307 [2004] => 140.5174 [2005] => 223.553276 [2006] => 195.462525 [2007] => 168.3059583333 [2008] => -39996 )
    Last edited by mith36; 07-30-2008 at 03:39 PM.

  5. #5
    Join Date
    Jul 2008
    Posts
    92
    After an arsort() of the above array, I get exactly what I need, but there's one last step I'd like to ask for a little discussion over.

    Say I have this array after the arsort():

    Array ( [1995] => 62.302 [1952] => 53.1367 [2005] => 52.5541698 [1965] => 50.4781 [1949] => 48.6891 [1962] => 48.2836 [1991] => 47.5 [1968] => 45.074 [1998] => 38.9842794 [1992] => 38.87 [1958] => 37.2245 [1960] => 35.4796 [1966] => 27.7315 [1957] => 27.6106 [1948] => 25.8672 [1969] => 23.9988 [1954] => 22.972 [1997] => 21.5524476 [2008] => 20 [2003] => 17.873064 [1964] => 17.7017 [2001] => 16.9328214 [1950] => 15.8067 [1953] => 14.9004 [1961] => 11.6318 [1956] => 9.1682 [1967] => 9.0444 [2004] => 8.5855968 [1951] => 7.6841 [1959] => 7.5266 [1970] => 7.0387 [1963] => 6.4756 [1999] => 3.955545 [1955] => 3.5711 [2000] => 1.9321434 [1971] => 0.985 [1972] => 0.8 [1994] => 0.36 [2002] => 0 [1996] => 0 [2006] => 0 [1947] => -29997 [1993] => -39996 [2007] => -39996 ) **neg numbs mean there was missing data and are ignored**

    of which I can generate this table:

    Rank Year Flow
    1 1995 62.30
    2 1952 53.14
    3 2005 52.55
    4 1965 50.48
    5 1949 48.69
    6 1962 48.28
    7 1991 47.50
    8 1968 45.07
    9 1998 38.98
    ....snip


    But what if I only want to display the current years rank and maybe 3 or 4 above and below the current years rank, plus the top one?

    I'd image I'd scan through, look for the rank of the current year, save it, then do display my table with +/- a value of the current years rank and just display that. I can do this simple enough, but I was curious if there was a way an expert would approach this with less work?

  6. #6
    Join Date
    Jul 2008
    Posts
    92
    I was able to finish this.

    Here's a sample output
    Code:
    Rank	    Year	Flow
    28	    1927	44.30
    29	    1996	43.35
    30	    1974	42.70
    31	    1969	42.41
    32	    1978	42.38
    Forecast    2008        42.00
    33	    1971	41.70
    34	    1929	41.40
    35	    1999	40.85
    36	    1967	40.57
    37	    1964	40.36
    If anyone stumbles upon this page at a later date trying a similar task, feel free to msg me for details.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles