Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to unset a group of array keys that have the same prefix. I can't seem to get this to work.

foreach ($array as $key => $value) {
    unset($array['prefix_' . $key]);
    }

How can I get unset to see ['prefix_' . $key] as the actual variable? Thanks

UPDATE: The $array keys will have two keys with the same name. Just one will have the prefix and there are about 5 keys with prefixed keys:

Array {
   [name] => name
   [prefix_name] => other name
}

I don't want to remove [name] just [prefix_name] from the array.

share|improve this question
    
Do you know the prefix? –  Nanne Oct 12 '11 at 20:36
    
You can't have two arrays with same name ($array) and different keys (one is with key $key and second one is with key "prefix_$key". You do it wrong. –  Dusan Oct 12 '11 at 20:37
    
@Brandon: As a gentle reminder, remember to go back to this and previous questions and mark the answer that worked for you as accepted. This allows future visitors to see the correct answer right away while also accrediting yourself and the poster for their time. –  Brad Christie Oct 13 '11 at 12:40
add comment

5 Answers

up vote 1 down vote accepted

This works:

$array = array(
  'aa' => 'other value aa',
  'prefix_aa' => 'value aa',
  'bb' => 'other value bb',
  'prefix_bb' => 'value bb'
);

$prefix = 'prefix_';
foreach ($array as $key => $value) {
  if (substr($key, 0, strlen($prefix)) == $prefix) {
     unset($array[$key]);
  }
}

If you copy/paste this code at a site like http://writecodeonline.com/php/, you can see for yourself that it works.

share|improve this answer
add comment

You can't use a foreach because it's only a copy of the collection. You'd need to use a for or grab the keys separately and separate your processing from the array you want to manipulate. Something like:

foreach (array_keys($array) as $keyName){
  if (strncmp($keyName,'prefix_',7) === 0){
    unset($array[$keyName]);
  }
}

You're also already iterating over the collection getting every key. Unless you had:

$array = array(
  'foo' => 1,
  'prefix_foo' => 1
);

(Where every key also has a matching key with "prefix_" in front of it) you'll run in to trouble.

share|improve this answer
    
The first entirely false. . . The key will be a copy, sure, but notice that he's indexing the original array, not the copy. –  Levi Morrison Oct 12 '11 at 20:46
add comment

I'm not sure I understand your question, but if you are trying to unset all the keys with a specific prefix, you can iterate through the array and just unset the ones that match the prefix.

Something like:

<?php
foreach ($array as $key => $value) {      // loop through keys
    if (preg_match('/^prefix_/', $key)) { // if the key stars with 'prefix_'
        unset($array[$key]);              // unset it
    }
}
share|improve this answer
add comment

You're looping over the array keys already, so if you've got

$array = (
    'prefix_a' => 'b',
    'prefix_c' => 'd'
     etc...
)

Then $keys will be prefix_a, prefix_c, etc... What you're doing is generating an entirely NEW key, which'd be prefix_prefix_a, prefix_prefix_c, etc...

Unless you're doing something more complicated, you could just replace the whole loop with

$array = array();
share|improve this answer
add comment

I believe this should work:

foreach ($array as $key => $value) {
    unset($array['prefix_' . str_replace('prefix_', '', $key]);
}
share|improve this answer
add comment

Your Answer

 
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.