Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional search on multidimensional array

Status
Not open for further replies.

waubain

Technical User
Dec 13, 2011
200
0
0
US
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.

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...
 
I found a solution that will work.

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...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top