Jump to content

Removing array elements based on a different array


jwhite68

Recommended Posts

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).

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

[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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.