Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Jan 2005
    Location
    Memphis, TN
    Posts
    1,801
    Thanks
    8
    Thanked 131 Times in 129 Posts

    A little array help please (getting the right key value)

    PHP Code:
    function subval_sort($a,$subkey,$sort) {
        foreach(
    $a as $k=>$v) {
            
    $b[$k] = strtolower($v[$subkey]);
        }
        
    $sort($b);
        foreach(
    $b as $key=>$val) {
            
    $c[] = $a[$key];
        }
        return 
    $c;
    }

    $calendars subval_sort($calendars,'realtime',asort);

    foreach(
    $calendars as $calendar1) {
        foreach (
    $calendar1 as $key=>$calendar2) {
            
    $todaysDate date(strtotime($startTime));
            if(
    $key == 'realtime'){
                
    // um...
            
    }
        }

    my array looks like:

    Code:
    Array
    (
        [0] => Array
            (
                [title] => Event A
                [when] => Fri May 27, 2011
                [realtime] => 1306472400
                [jstime] => May.27.2011
            )
    
        [1] => Array
            (
                [title] => Memorial Day
                [when] => Mon May 30, 2011
                [realtime] => 1306731600
                [jstime] => May.30.2011
            )
    
    etc...
    I;m using the 'realtime' in order to sort by date as $calendars is actually 5 arrays merged into one. The 'jstime' is the same date but formatted so I can apply my styling.

    So what I need to do is get today's date (got it), then check all the items n the array that have the same time as today and echo out the 'title' from each array. That's this section:

    PHP Code:
    foreach($calendars as $calendar1) {
        foreach (
    $calendar1 as $key=>$calendar2) {
            
    $todaysDate date(strtotime($startTime));
            if(
    $key == 'realtime'){
                
    // um...
            
    }
        }

    a little lost as to what to write to grab each title in the array that has a 'realtime' of today.
    Stop making things so hard on yourself.
    i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Write a filter for it, its way faster. Then you needn't worry about a conditional check.
    Filter's have explicit signatures, so I'll be using a global variable for the realtime.

    PHP Code:
    $realtime 1306472400// Calculate your day
    function filterCalendarToday($value)
    {
        global 
    $realtime;
        
    $bResult false;
        if (isset(
    $value['realtime']) && $value['realtime'] == $realtime)
        {
            
    $bResult true;
        }
        return 
    $bResult;
    }

    $calendars // how you got your array
    $aFilteredCalendars array_filter($calendars'filterCalendarToday');
    foreach (
    $aFilteredCalendars AS $value)
    {
        print 
    $value['title'] . PHP_EOL;

    How you get the $realtime is really up to you. You can drop the whole global by working explicitly on the time (strtotime with todays date puts it ad midnight), which can be a huge help. The $realtime is a convenience variable.

  • Users who have thanked Fou-Lu for this post:

    harbingerOTV (05-26-2011)

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    If you know the name of the key you're looking for there is no need to loop a second time:
    PHP Code:
    $todaysDate strtotime($startTime); 
    foreach(
    $calendars as $calendar)
    {
        if(isset(
    $calendar['realtime']) && $calendar['realtime'] == $todaysDate)
        {
            echo 
    $calendar['title'];
        }

    Unless I'm missing something.

  • Users who have thanked Inigoesdr for this post:

    harbingerOTV (05-26-2011)

  • #4
    Senior Coder
    Join Date
    Jan 2005
    Location
    Memphis, TN
    Posts
    1,801
    Thanks
    8
    Thanked 131 Times in 129 Posts
    Spot on both of you. Inigoesdr's is easier to look at so I went that route as this will no doubt, fall into someone else hands someday. Easier to comment.

    Once again it looks like I was close but, well, horseshoes and hand grenades
    Stop making things so hard on yourself.
    i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis


  •  

    Posting Permissions

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