Suppose I have first array, $aAllCities as

Array
(
   [21] => London
   [9]  => Paris
   [17] => New York
   [3]  => Tokyo
   [25] => Shanghai
   [11] => Dubai
   [37] => Mumbai
)

And another array, $aNotSupportedCities as

Array
(
   [0] => 37
   [1] => 25
   [2] => 11
)

Is it possible to get an array like this ?

Array
(
   [21] => London
   [9]  => Paris
   [17] => New York
   [3]  => Tokyo
)

I want to remove array values of those keys that are present in other array

share|improve this question

62% accept rate
feedback

5 Answers

up vote 1 down vote accepted

Try this:

$aAllCities = array_flip( $aAllCities );
$aAllCities = array_diff( $aAllCities, $aNotSupportedCities );
$aAllCities = array_flip( $aAllCities );

Hope this helps.

share|improve this answer
Thanks Pushpesh, exactly what I wanted – Sachyn Apr 19 at 7:45
feedback
$new = $aAllCities;
foreach($aNotSupportedCities as $id) {
  if (isset($new[$id]) {
    unset($new[$id]);
  }
}
share|improve this answer
feedback
foreach($aAllCities as $key => $value) {
    if(in_array($key,$aNotSupportedCities)) {
        unset($aAllCities[$key]); 
    }

}
share|improve this answer
feedback
$supportedCities = array_diff_key($aAllCities, array_values($aNotSupportedCities));
share|improve this answer
feedback

The other answers are correct, but a smoother, faster way to do it is:
$supportedCities = array_diff_key($aAllCities, $aNotSupportedCities);

This will return all the values from $aAllCities that don't have keys in $aNotSupportedCities

Note, this compares the two arrays via their keys, so you will need to make your $aNotSupportedCities look like this:

Array
(
   [37] => something
   [25] => doesn't really matter
   [11] => It's not reading this
)

Best of luck.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.