如何删除数组键中的前缀

发布于 2024-12-22 18:03:41 字数 197 浏览 0 评论 0原文

我尝试删除数组键中的前缀,但每次尝试都失败。我想要实现的是:

拥有: Array ( [attr_Size] => 3 [attr_Colour] => 7 )

获取: Array ( [Size] => 3 [颜色] => 7 )

非常感谢您的帮助...

I try to remove a prefix in array keys and every attempt is failing. What I want to achieve is to:

Having: Array ( [attr_Size] => 3 [attr_Colour] => 7 )

To Get: Array ( [Size] => 3 [Colour] => 7 )

Your help will be much appreciated...

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

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

发布评论

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

评论(3

月下客 2024-12-29 18:03:41

获取方法之一:Array ( [Size] => 3 [Colour] => 7 ) 从您的 Having:Array ( [attr_Size] => 3 [attr_Colour] => 7 )

$new_arr = array();
foreach($Your_arr as $key => $value) {

list($dummy, $newkey) = explode('_', $key);
$new_arr[$newkey] = $value;

}

如果您认为键中会有多个下划线,只需将 foreach 中的第一行替换为 list($dummy, $newkey) = 爆炸('attr_', $key);

One of the ways To Get:Array ( [Size] => 3 [Colour] => 7 ) From your Having: Array ( [attr_Size] => 3 [attr_Colour] => 7 )

$new_arr = array();
foreach($Your_arr as $key => $value) {

list($dummy, $newkey) = explode('_', $key);
$new_arr[$newkey] = $value;

}

If you think there'll be multiple underscores in keys just replace first line inside foreach with list($dummy, $newkey) = explode('attr_', $key);

ぺ禁宫浮华殁 2024-12-29 18:03:41

如果我理解你的问题,你就不必使用 implode() 来得到你想要的。

define(PREFIX, 'attr_');

$array = array('attr_Size' => 3, 'attr_Colour' => 7);

$prefixLength = strlen(PREFIX);

foreach($array as $key => $value)
{
  if (substr($key, 0, $prefixLength) === PREFIX)
  {
    $newKey = substr($key, $prefixLength);
    $array[$newKey] = $value;
    unset($array[$key]);
  }
}

print_r($array); // shows: Array ( [Size] => 3 [Colour] => 7 ) 

If I understood your question, you don't have to use implode() to get what you want.

define(PREFIX, 'attr_');

$array = array('attr_Size' => 3, 'attr_Colour' => 7);

$prefixLength = strlen(PREFIX);

foreach($array as $key => $value)
{
  if (substr($key, 0, $prefixLength) === PREFIX)
  {
    $newKey = substr($key, $prefixLength);
    $array[$newKey] = $value;
    unset($array[$key]);
  }
}

print_r($array); // shows: Array ( [Size] => 3 [Colour] => 7 ) 
莳間冲淡了誓言ζ 2024-12-29 18:03:41

因为您想要在每个键中保留的第一个字符以大写字母开头,所以您可以简单地向左修剪小写字母和下划线,瞧。要创建所有小写字母和下划线的“掩码”,您可以使用 a..z_,但由于 attr_ 是已知前缀,_art 就可以了。诚然,我的代码片段非常适合询问者的示例数据,不会调用 explode() 来创建临时数组,并且每次迭代不会进行多个函数调用。有争议地使用。

代码:(演示

$array = [
    'attr_Size' => 3,
    'attr_Colour' => 7
];

$result = [];
foreach ($array as $key => $value) {
    $result[ltrim($key, '_art')] = $value;
}
var_export($result);

输出:

array (
  'Size' => 3,
  'Colour' => 7,
)

Because the first character that you'd like to retain in each key starts with an uppercase letter, you can simply left-trim lowercase letters and underscores and voila. To create a "mask" of all lowercase letters and the underscore, you could use a..z_, but because attr_ is the known prefix, _art will do. My snippet, admittedly, is narrowly suited to the asker's sample data, does not call explode() to create a temporary array, and does not make multiple function calls per iteration. Use contentiously.

Code: (Demo)

$array = [
    'attr_Size' => 3,
    'attr_Colour' => 7
];

$result = [];
foreach ($array as $key => $value) {
    $result[ltrim($key, '_art')] = $value;
}
var_export($result);

Output:

array (
  'Size' => 3,
  'Colour' => 7,
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文