#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Location
    Fresno, CA
    Posts
    13
    Rep Power
    0

    Challenging Array Problem


    I have an array with key / values stored as:
    [August, 2009] => 0
    [September, 2009] => 0
    [October, 2009] => 0
    etc...
    One key value pair for each month from August of previous year to the current month. I loop through the array fine to display it on a type of calendar. But a problem occurs when the array does not contain a month entry.

    I need to be able to examine the array, find which month is missing and fill it in in the proper order.

    All help is appreciated, any nudge in the right direction will help.
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,472
    Rep Power
    2106
    The best way that I can think of to do this is to start with another array that holds the months that you need, and then look through that and check if there is a corresponding value in your data array. If there is, you can use that, if not, display whatever error/blank message that you want to. A (very) basic idea is something like this...
    PHP Code:
    $data = array (
        
    "October 2009" => 5,
        
    "September 20099" => 2,
        
    "November 2009" => 5
    );

    $months = array (
        
    "October 2009",
        
    "September 2009",
        
    "October 2009"
        
    "November 2009"
    );

    foreach (
    $months as $month) {
        if (isset (
    $data [$month])) {
            echo 
    "<p>We have found ".$data [$month]." for ".$month."</p>";
        }
        else {
            echo 
    "<p>No data for ".$month."</p>";
        }

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    425
    Rep Power
    0
    print_r ($array)

    Comments on this post

    • requinix disagrees : don't see how that helps
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Location
    Mars
    Posts
    5
    Rep Power
    0
    key_exists() is more appropriate

    PHP Code:
    $data = array (
        
    "October 2009" => 5,
        
    "September 20099" => 2,
        
    "November 2009" => 5
    );

    $months = array (
        
    "October 2009",
        
    "September 2009",
        
    "October 2009"
        
    "November 2009"
    );

    foreach (
    $months as $month) {
      if (
    key_exists ($month,$data)) {
            echo 
    "<p>We have found ".$data [$month]." for ".$month."</p>";
        }
        else {
            echo 
    "<p>No data for ".$month."</p>";
        }



    Originally Posted by Catacaustic
    The best way that I can think of to do this is to start with another array that holds the months that you need, and then look through that and check if there is a corresponding value in your data array. If there is, you can use that, if not, display whatever error/blank message that you want to. A (very) basic idea is something like this...
    PHP Code:
    $data = array (
        
    "October 2009" => 5,
        
    "September 20099" => 2,
        
    "November 2009" => 5
    );

    $months = array (
        
    "October 2009",
        
    "September 2009",
        
    "October 2009"
        
    "November 2009"
    );

    foreach (
    $months as $month) {
        if (isset (
    $data [$month])) {
            echo 
    "<p>We have found ".$data [$month]." for ".$month."</p>";
        }
        else {
            echo 
    "<p>No data for ".$month."</p>";
        }

  8. #5
  9. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,276
    Rep Power
    4193
    Originally Posted by Catacaustic
    The best way that I can think of to do this is to start with another array that holds the months that you need, and then look through that and check if there is a corresponding value in your data array. If there is, you can use that, if not, display whatever error/blank message that you want to. A (very) basic idea is something like this...
    Rather than having two arrays in use during display, I'd just build the original array with all valid values, then over-write the values with the data from the database or similar.

    Ie:
    php Code:
     
    <?php
    $data = array();
    $endMonth = date('n');
    $startMonth=8;
     
    $endYear=$startYear = date('Y');
    if ($startMonth < 8) $startYear--;
     
    $data=array();
    while ($startMonth <= $endMonth && $startYear <= $endYear){
       $startMonth++;
       if ($startMonth > 12){ $startMonth=1; $startYear++; }
     
       $date = mktime(0, 0, 0, $startMonth, 1, $startYear);
       $data[date('F, Y')] = 0;
    }
     
    //now, fill $data
     
    //then print it.
    ?>

    Comments on this post

    • aknatn agrees : This helped, thanks!
    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Location
    Fresno, CA
    Posts
    13
    Rep Power
    0
    Thanks but it ultimately led to management requesting a different route for data display. But you really helped me visualize pre-populating arrays in preparation for MySql results. Have a great weekend!

IMN logo majestic logo threadwatch logo seochat tools logo