php 如何删除数组键中的前缀

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8614397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:05:43  来源:igfitidea点击:

How to remove prefix in array keys

phparrayskeyimplode

提问by rat4m3n

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 )

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

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

要得到: Array ( [Size] => 3 [Colour] => 7 )

Your help will be much appreciated...

您的帮助将不胜感激...

采纳答案by TigerTiger

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

获取方式之一: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;

}

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

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

回答by Frosty Z

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

如果我理解你的问题,你就不必使用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 ) 

回答by Ligemer

Here's something else to chew on that can be reused for multiple arrays in your application that have different key prefixes. This would be useful if you have Redis prefixed keys to remap or something of that nature.

这里还有一些需要注意的东西,它可以重用于应用程序中具有不同键前缀的多个数组。如果你有 Redis 前缀的键来重新映射或类似的东西,这将很有用。

$inputArray = array('attr_test' => 'test', 'attr_two' => 'two');

/**
 * Used to remap keys of an array by removing the prefix passed in
 *
 * Example:
 * $inputArray = array('app_test' => 'test', 'app_two' => 'two');
 * $keys = array_keys($inputArray);
 * array_walk($keys, 'removePrefix', 'app_');
 * $remappedArray = array_combine($keys, $inputArray);
 *
 * @param $value - key value to replace, should be from array_keys
 * @param $omit - unused, needed for prefix call
 * @param $prefix - prefix to string replace in keys
 */
function removePrefix(&$value, $omit, $prefix) {
    $value = str_replace($prefix, '', $value);
}

// first get all the keys to remap
$keys = array_keys($inputArray);

// perform internal iteration with prefix passed into walk function for dynamic replace of key
array_walk($keys, 'removePrefix', 'attr_');

// combine the rewritten keys and overwrite the originals
$remappedArray = array_combine($keys, $inputArray);

// see full output of comparison
var_dump($inputArray);
var_dump($remappedArray);

Output:

输出:

array(2) {
  'attr_test' =>
  string(4) "test"
  'attr_two' =>
  string(3) "two"
}
array(2) {
  'test' =>
  string(4) "test"
  'two' =>
  string(3) "two"
}