简体   繁体   English

PHP数组未设置字符串

[英]PHP array unset string

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_' . 我该如何取消查看['prefix_'。 $key] as the actual variable? $ key]作为实际变量? Thanks 谢谢

UPDATE: The $array keys will have two keys with the same name. 更新:$ array键将有两个具有相同名称的键。 Just one will have the prefix and there are about 5 keys with prefixed keys: 只有一个具有前缀,并且大约有5个带有前缀键的键:

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

I don't want to remove [name] just [prefix_name] from the array. 我不想从数组中删除[name]只是[prefix_name]。

You can't use a foreach because it's only a copy of the collection. 您不能使用foreach,因为它只是集合的一个副本。 You'd need to use a for or grab the keys separately and separate your processing from the array you want to manipulate. 您需要单独使用for或抓住键,并将处理与要操纵的阵列分开。 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. (如果每个键前面都有一个匹配的键,前面带有“ prefix_”),您将遇到麻烦。

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

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. 如果您在http://writecodeonline.com/php/之类的网站上复制/粘贴此代码,则可以亲眼看到它的工作原理。

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... 然后$ keys将是prefix_aprefix_c等...您正在做的是生成一个全新的密钥,它将是prefix_prefix_aprefix_prefix_c等。

Unless you're doing something more complicated, you could just replace the whole loop with 除非您要进行更复杂的操作,否则只需将整个循环替换为

$array = array();

I believe this should work: 我相信这应该有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM