0

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.

2
  • 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. Commented Oct 12, 2011 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. Commented Oct 13, 2011 at 12:40

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

The first entirely false. . . The key will be a copy, sure, but notice that he's indexing the original array, not the copy.
1

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

Comments

0

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();

Comments

0

I believe this should work:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.