connor11493 Posted December 8, 2012 Share Posted December 8, 2012 Good afternoon everyone, and happy holidays! Although my membership might not qualify as "new" to this forum, I've never used my account here before but today I think I finally found a reason to! I'm trying to exclude four (4) values from array, which will then echo the remaining values into a drop-down box on a form. Here is the contents of the array: Array ( [0] => stdClass Object ( [id] => 22 [icao] => CYQM [name] => Greater Moncton International Airport - CHARTER [country] => Canada [lat] => 46.1122 [lng] => -64.6786 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [1] => stdClass Object ( [id] => 19 [icao] => EINN [name] => Shannon International Airport - CHARTER [country] => Ireland [lat] => 52.702 [lng] => -8.92482 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [2] => stdClass Object ( [id] => 16 [icao] => KAUS [name] => Austin-Bergstrom International Airport [country] => United States [lat] => 30.1945 [lng] => -97.6699 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [3] => stdClass Object ( [id] => 9 [icao] => KDEN [name] => Denver International Airport [country] => United States [lat] => 39.8583 [lng] => -104.667 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [4] => stdClass Object ( [id] => 2 [icao] => KLAS [name] => McCarran International Airport [country] => United States [lat] => 36.0827 [lng] => -115.154 [hub] => 1 [fuelprice] => 0 [chartlink] => ) [5] => stdClass Object ( [id] => 5 [icao] => KLGB [name] => Long Beach Airport [country] => United States [lat] => 33.8175 [lng] => -118.152 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [6] => stdClass Object ( [id] => 14 [icao] => KOAK [name] => Metropolitan Oakland International Airport [country] => United States [lat] => 37.721 [lng] => -122.222 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [7] => stdClass Object ( [id] => 10 [icao] => KPDX [name] => Portland International Airport [country] => United States [lat] => 45.5887 [lng] => -122.598 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [8] => stdClass Object ( [id] => 3 [icao] => KPHX [name] => Phoenix Sky Harbor International Airport [country] => United States [lat] => 33.4359 [lng] => -112.01 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [9] => stdClass Object ( [id] => 12 [icao] => KRNO [name] => Reno/Tahoe International Airport [country] => United States [lat] => 39.4991 [lng] => -119.768 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [10] => stdClass Object ( [id] => 4 [icao] => KSAN [name] => San Diego International Airport [country] => United States [lat] => 32.7334 [lng] => -117.188 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [11] => stdClass Object ( [id] => 17 [icao] => KSAT [name] => San Antonio International Airport [country] => United States [lat] => 29.5338 [lng] => -98.47 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [12] => stdClass Object ( [id] => 6 [icao] => KSEA [name] => Seattle-Tacoma International Airport [country] => United States [lat] => 47.449 [lng] => -122.309 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [13] => stdClass Object ( [id] => 8 [icao] => KSFO [name] => San Francisco International Airport [country] => United States [lat] => 37.6188 [lng] => -122.376 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [14] => stdClass Object ( [id] => 15 [icao] => KSJC [name] => San Jose International Airport [country] => United States [lat] => 37.3619 [lng] => -121.929 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [15] => stdClass Object ( [id] => 11 [icao] => KSLC [name] => Salt Lake City International Airport [country] => United States [lat] => 40.7884 [lng] => -111.978 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [16] => stdClass Object ( [id] => 7 [icao] => KSMF [name] => Sacramento International Airport [country] => United States [lat] => 38.6952 [lng] => -121.592 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [17] => stdClass Object ( [id] => 13 [icao] => KTNT [name] => Dade-Collier Training Airport - CHARTER [country] => United States [lat] => 25.862 [lng] => -80.8967 [hub] => 0 [fuelprice] => 0 [chartlink] => ) [18] => stdClass Object ( [id] => 18 [icao] => LTAI [name] => Antalya International Airport - CHARTER [country] => Turkey [lat] => 36.8987 [lng] => 30.8005 [hub] => 0 [fuelprice] => 0 [chartlink] => ) ) 1 As you can see, it's one (1) array that's broken up into a separate stdClass Object() array of some sort. My goal here is to exclude the following [id] values from the set of arrays above: 13, 18, 19, 22 The problem is that each array key (ex. [1], [2], [3]) from the first array will change when the contents of the array is increased over time. Therefore, I cannot simply exclude array keys [1], [2], [17], [18] Here's the code I'm working with now: <div id="depapttab"> <p>Select your departure airport:</p> <select id="depicao" name="depicao"> <option value="">Select All</option> <?php if(!$depairports) $depairports = array(); foreach($depairports as $airport) { if($depairports->id != array(13,18,19,22)) { echo '<option value="'.$airport->icao.'">'.$airport->icao.' - '.$airport->name.'</option>'; } } ?> </select> <input type="submit" name="submit" value="Find Flights" /> </div> I just don't know how to include that darn stdClass Object() in my if() statement. If someone could lend a hand, it'd be much appreciated! Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/271760-need-help-removing-values-from-array/ Share on other sites More sharing options...
Andy123 Posted December 9, 2012 Share Posted December 9, 2012 Close. Right now you are testing "if the ID is not equal to an array consisting of the elements 13, 18, 19 and 22". What you really want to do is to check if the ID is not either one of the elements 13, 18, 19 or 22. You can do this by using PHP's in_array function. This function returns true if a given value is in the passed array. $exclude = array(13, 18, 19, 22); foreach($depairports as $airport) { if (!in_array($depairports->id, $exclude)) { // This airport's ID is not 13, 18, 19 or 22 and should thus be added to the dropdown } } Quote Link to comment https://forums.phpfreaks.com/topic/271760-need-help-removing-values-from-array/#findComment-1398301 Share on other sites More sharing options...
connor11493 Posted December 9, 2012 Author Share Posted December 9, 2012 Thanks for the insight, Andy! Unfortunately, I didn't have much success. Perhaps I need to clarify a little bit: The dropdown menu I'm trying to edit is a part of a CM tool I'm using that has a few classes and modules that help it run more efficiently. This particular dropdown menu is located in schedule_searchform.tpl which references module Schedules.php Schedules.php contains the following code: public function showSchedules() { $airports = OperationsData::GetAllAirports(); $exclude = array(13, 18, 19, 22); $departs = array_diff($airports, $exclude); $equip = OperationsData::GetAllAircraftSearchList(true); $reg = OperationsData::getAllRegistrationSearchList(true); $this->set('depairports', $depapts); $this->set('equipment', $equip); $this->set('registration', $reg); $this->render('schedule_searchform.tpl'); # Show the routes. Remote this to not show them. $this->set('allroutes', SchedulesData::GetSchedules()); $this->render('schedule_list.tpl'); } As you can see, I tried implementing your code from the module this form is referencing. However, now that I've done this, I'm experiencing the following error message: Catchable fatal error: Object of class stdClass could not be converted to string in /home4/flybl110/public_html/core/modules/Schedules/Schedules.php on line 163 This is what gives me reason to believe I'm not connecting the array() function with the stdClass Object() string correctly. Bummer Quote Link to comment https://forums.phpfreaks.com/topic/271760-need-help-removing-values-from-array/#findComment-1398308 Share on other sites More sharing options...
Barand Posted December 9, 2012 Share Posted December 9, 2012 Close. $exclude = array(13, 18, 19, 22); foreach($depairports as $airport) { if (!in_array($depairports->id, $exclude)) { // This airport's ID is not 13, 18, 19 or 22 and should thus be added to the dropdown } } You should use $airport->id and not $depairports->id Quote Link to comment https://forums.phpfreaks.com/topic/271760-need-help-removing-values-from-array/#findComment-1398338 Share on other sites More sharing options...
Andy123 Posted December 9, 2012 Share Posted December 9, 2012 You should use $airport->id and not $depairports->id Oh boy, what a silly mistake. I let myself influence too much by the existing if sentence. Thanks for catching this! Quote Link to comment https://forums.phpfreaks.com/topic/271760-need-help-removing-values-from-array/#findComment-1398350 Share on other sites More sharing options...
connor11493 Posted December 9, 2012 Author Share Posted December 9, 2012 Barand and Andy, I can't thank you guys enough. That original script Andy gave me yesterday with Barand's modification worked just how I wanted it to I can't believe I didn't spot that mistake myself! Anyway, if it weren't for your original idea, Andy, this would have never worked. Thanks again, guys! I'm eager to return the favor whenever you might need a hand Quote Link to comment https://forums.phpfreaks.com/topic/271760-need-help-removing-values-from-array/#findComment-1398381 Share on other sites More sharing options...
Andy123 Posted December 9, 2012 Share Posted December 9, 2012 You are very welcome - happy to help. Quote Link to comment https://forums.phpfreaks.com/topic/271760-need-help-removing-values-from-array/#findComment-1398385 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.