jwhite68 Posted July 3, 2007 Share Posted July 3, 2007 I'll illustrate my issue with a simpler example. Suppose I have an array called countries with entries: $countries["us"] = "United States"; $countries["ca"] = "Canada"; $countries["al"] = "Albania"; $countries["dz"] = "Algeria"; Now, lets suppose I have another array, country2, which has the following: $countries2["ca"] = "Canada"; $countries2["al"] = "Albania"; I effectivelty want a way to remove the array elements that are in country2, from the country array. and produce a new resultant array $country_list. So after processing, what I am left with is the following (for this example): $country_list["us"] = "United States"; $country_list["dz"] = "Algeria"; Does anyone have any advice, sample code that could achieve this? In the actual code, country2 will change dynamically - ie the countries that need removing will change each time the form loads (or may change). Quote Link to comment Share on other sites More sharing options...
trq Posted July 3, 2007 Share Posted July 3, 2007 <?php $country_list = array_diff($countries,$country2); ?> Quote Link to comment Share on other sites More sharing options...
jwhite68 Posted July 3, 2007 Author Share Posted July 3, 2007 Thanks, but I just realised something. My country2 array is actually not in the same format. Its like this: [0] => ai [1] => au [2] => ca [3] => us So its values contain the 'keys' for the index of the other array, namely - $countries. How would I get the same result, with this situation? Quote Link to comment Share on other sites More sharing options...
jwhite68 Posted July 3, 2007 Author Share Posted July 3, 2007 [code]I am trying to achieve the result with the following code: foreach ($countries as $key2 => $value2) { $dont_output=0; foreach ($country2 as $key3 => $value3) if (array_key_exists('"'.$value3.'"',$countries)) dont_output=1; if ($dont_output==0) echo '<option value="'.$key2.'">'.$countries["$key2"].'</option>'; } [/code] What I am actually trying to do, is build a drop down list of 'option values', from $countries, but ensuring that any entries that are in $country2 do not appear in the output. Since the values of the array elements in $country2 are the key/index values for $country, I thought I would be able to use the array_key_exists function to compare a value of $country2, against the index values in $countries. However, at runtime, this always outputs the whole contents of $countries still, via the option value statements. ie. $dont_output is never set to 1. Quote Link to comment Share on other sites More sharing options...
jwhite68 Posted July 3, 2007 Author Share Posted July 3, 2007 I was able to resolve by changing: if (array_key_exists('"'.$value3.'"',$countries)) dont_output=1; to if ($value3==$key2) $dont_output=1; It now works. Although I am sure there must be a quicker and neater way in PHP... Quote Link to comment Share on other sites More sharing options...
per1os Posted July 3, 2007 Share Posted July 3, 2007 <?php foreach ($countries as $key => $value) { if (!in_array($key, $country2)) { echo '<option value="'.$key.'">'.$value].'</option>'; } } ?> Your logic was all FUBAR'ed up. EDIT: Updated the function to do it correctly. Quote Link to comment 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.