Jump to content

Need Help Removing Values From Array


connor11493
 Share

Recommended Posts

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!

Link to comment
Share on other sites

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
}
}

Link to comment
Share on other sites

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 :huh:

Link to comment
Share on other sites

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! :lol: 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 ^_^

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.