PHP 数组未设置字符串

发布于 2024-12-09 16:02:42 字数 387 浏览 0 评论 0原文

我正在尝试取消设置一组具有相同前缀的数组键。我似乎无法让它发挥作用。

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

我怎样才能取消设置以查看 ['prefix_' . $key] 作为实际变量?谢谢

更新: $array 键将有两个同名的键。只有一个有前缀,大约有 5 个带有前缀键的键:

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

我不想从数组中删除 [name] 只是 [prefix_name]。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

那片花海 2024-12-16 16:02:42

这有效:

$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]);
  }
}

如果您在 http://writecodeonline.com/php/ 之类的网站复制/粘贴此代码,您可以亲眼看看它是否有效。

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.

睫毛上残留的泪 2024-12-16 16:02:42

您不能使用 foreach,因为它只是集合的副本。您需要使用 for 或单独获取键,并将处理与要操作的数组分开。类似于:

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

您也已经在迭代集合以获得每个键。除非你有:(

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

每个键也有一个前面带有“prefix_”的匹配键)你会遇到麻烦。

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.

-黛色若梦 2024-12-16 16:02:42

我不确定我是否理解您的问题,但如果您尝试取消设置具有特定前缀的所有键,您可以迭代数组并仅取消设置与前缀匹配的键。

像这样的东西:

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

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
    }
}
花开半夏魅人心 2024-12-16 16:02:42

您已经在遍历数组键了,所以如果您有

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

那么 $keys 将是 prefix_aprefix_c 等...您正在做的是生成一个全新的密钥,即 prefix_prefix_aprefix_prefix_c 等...

除非您正在做更复杂的事情,否则您可以将整个循环替换为

$array = array();

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();
触ぅ动初心 2024-12-16 16:02:42

我相信这应该有效:

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

I believe this should work:

foreach ($array as $key => $value) {
    unset($array['prefix_' . str_replace('prefix_', '', $key]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文