×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Conditional search on multidimensional array

Conditional search on multidimensional array

Conditional search on multidimensional array

(OP)
I am trying to use a multidimensional array for the first time, but having trouble getting the conditional search for "//Find next meeting date" section.

Any help would be appreciated. Thank you.

CODE --> PHP

//today's date variable
        date_default_timezone_set("America/Chicago");    
        $todaysdate = date("Ymd");        //Date format as YYYYMMDD

        //create multidimentional array
        $meetinfo = Array
        (
          array('meetdate' => "20190110", 'speaker' => "Bob", 'room' => "Classroom #1", 'ce' => 1.0),  
          array('meetdate' => "20180512", 'speaker' => "Joe", 'room' => "Classroom #3", 'ce' => 1.0), 
          array('meetdate' => "20180820", 'speaker' => "Sally", 'room' => "Classroom #6", 'ce' => 1.0),  
          );

        // Sort the multidimensional array
        usort($meetinfo, "custom_sort");
        // Define the custom sort function
        function custom_sort($a,$b) {
          return $a['meetdate']>$b['meetdate'];
        }

        //find next meeting date
       for ($index = 0; $index < count($meetinfo); $index++){
            if ($todaysdate <= $meetinfo["meetdate"][$index]) {                                                          
                $nextmeetdate = $meetdate["meetdate"][$index]; 
                $nextmeetdate = date("l F d, Y", strtotime($nextmeetdate)); //format date
                $speaker = $meetdate["speaker"];
                $room = $meetdate["room"];
                $ce = $meetdate["ce"];
                break;  //exit loop
            } else {
                    $nextmeetdate = "TBA";
                    $speaker = "TBA";
                    $room = "TBA";
                    $ce = "TBA";
                    }
        }  

        //show data 
        echo "<p><strong>" .$nextmeetdate. "</strong>"; echo "<br />";
        echo "Speaker: " .$speaker. ""; echo "<br>";
        echo "Topic: " .$topic. ""; echo "<br />";
        echo "Location: " .$location. "";echo "<br />";
        echo "CPE CE: " .$ce. "contact hour"; echo "<br />";
        echo "</p>"; 

You don't know what you don't know...

RE: Conditional search on multidimensional array

(OP)
I found a solution that will work.

CODE --> PHP

$buffer = array();
        foreach ($meetinfo as $element) {
            if ($todaysdate <= ($element['meetdate'])) {               
                $buffer[] = $element;
                $nextmeetdate = $buffer[0]['meetdate']; 
                $nextmeetdate = date("l F d, Y", strtotime($nextmeetdate)); //format date
                $speaker = $buffer[0]["speaker"];
                $room = $buffer[0]["room"];
                $ce = $buffer[0]["ce"];
                break;  //exit loop
                } 
                    else {
                        $nextmeetdate = "TBA";
                        $speaker = "TBA";
                        $room = "TBA";
                        $ce = "TBA";
                    }
          } 

You don't know what you don't know...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close