php 更改 array_walk 函数中的数组键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13271015/
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
changing array keys in array_walk function?
提问by mithunsatheesh
I am using array functions to convert my pipe delimited string to an associative array.
我正在使用数组函数将我的管道分隔字符串转换为关联数组。
$piper = "|k=f|p=t|e=r|t=m|";
$piper = explode("|",$piper);
$piper = array_filter($piper);
function splitter(&$value,$key) {
$splitted = explode("=",$value);
$key = $splitted[0];
$value = $splitted[1];
}
array_walk($piper, 'splitter');
var_dump($piper);
this gives me
这给了我
array (size=4)
1 => string 'f' (length=1)
2 => string 't' (length=1)
3 => string 'r' (length=1)
4 => string 'm' (length=1)
where i want:
我想要的地方:
array (size=4)
"k" => string 'f' (length=1)
"p" => string 't' (length=1)
"e" => string 'r' (length=1)
"t" => string 'm' (length=1)
but the keys are unaltered. Is there any array function with which i can loop over an array and change keys and values as well?
但键是不变的。是否有任何数组函数可以循环遍历数组并更改键和值?
回答by raina77ow
It's said in the documentation of array_walk(describing the callback function):
在array_walk的文档(描述回调函数)中是这样说的:
Only the values of the array may potentially be changed; its structure cannot be altered, i.e., the programmer cannot add, unset or reorder elements. If the callback does not respect this requirement, the behavior of this function is undefined, and unpredictable.
只有数组的值可能会改变;它的结构不能改变,即程序员不能添加、取消设置或重新排序元素。如果回调不遵守此要求,则此函数的行为未定义且不可预测。
That means you cannot use array_walkto alter the keys of iterated array. You can, however, create a new array with it:
这意味着您不能用于array_walk更改迭代数组的键。但是,您可以使用它创建一个新数组:
$result = array();
array_walk($piper, function (&$value,$key) use (&$result) {
$splitted = explode("=",$value);
$result[ $splitted[0] ] = $splitted[1];
});
var_dump($result);
Still, I think if it were me, I'd use regex here (instead of "exploding the exploded"):
尽管如此,我认为如果是我,我会在这里使用正则表达式(而不是“爆炸爆炸”):
$piper = "|k=f|p=t|e=r|t=m|";
preg_match_all('#([^=|]*)=([^|]*)#', $piper, $matches, PREG_PATTERN_ORDER);
$piper = array_combine($matches[1], $matches[2]);
var_dump($piper);
回答by hakre
You can better use foreachfor that. The following example shows processing an entry, adding it with the right key and deleting the original entry.
你可以更好地使用foreach它。以下示例显示处理条目,使用右键添加它并删除原始条目。
$piper = "|k=f|p=t|e=r|t=m|";
$piper = array_filter(explode("|", $piper));
foreach ($piper as $index => $value) {
list($key, $value) = explode("=", $value);
$piper[$key] = $value;
unset($piper[$index]);
}
Take care you do not have keys that are like an index.
注意你没有像索引一样的键。
Another alternative is to process the values via a reference and set the keys afterwards:
另一种选择是通过引用处理值,然后设置键:
foreach ($piper as &$value) {
list($keys[], $value) = explode("=", $value);
}
unset($value);
$piper = array_combine($keys, $piper);
This does not bring you into any troubles but just with duplicate keys. But you could check for that problem after the foreach, no data would be lost.
这不会给您带来任何麻烦,而只是使用重复的密钥。但是您可以在 之后检查该问题foreach,不会丢失任何数据。
Something that can not be guaranteed with the following foreachwhich probably is the most simplified by stroing into a result array:
以下内容无法保证,foreach这可能是通过 stroing 到结果数组中最简化的:
$result = array();
foreach ($piper as $value) {
list($key, $value) = explode("=", $value);
$result[$key] = $value;
}
回答by hakre
Why not build a new array that has the desired keys and values from $piper?
为什么不从 $piper 构建一个具有所需键和值的新数组?
$piper2 = array();
foreach ($piper as $k => $val)
{
$splitted = explode("=", $val);
$key = $splitted[0];
$value = $splitted[1];
$piper2[$key] = $value;
}
$piper = $piper2; // if needed
回答by B. Assem
Using array_reduce will do the trick
使用 array_reduce 可以解决问题
$piper = "|k=f|p=t|e=r|t=m|";
$piper = explode("|",$piper);
$piper = array_filter($piper);
function splitter($result, $item) {
$splitted = explode("=",$item);
$key = $splitted[0];
$value = $splitted[1];
$result[$key] = $value;
return $result;
}
$piper = array_reduce($piper, 'splitter', array());
var_dump($piper);
based on this: http://www.danielauener.com/howto-use-array_map-on-associative-arrays-to-change-values-and-keys/
基于此:http: //www.danielauener.com/howto-use-array_map-on-associative-arrays-to-change-values-and-keys/
回答by James Bond
This my recursive function that can change not only the values of the array as array_walk_recursive() but also the keysof the given array. It also keeps the order of the array: https://stackoverflow.com/a/57622225/10452175
这是我的递归函数,它不仅可以将数组的值更改为 array_walk_recursive() ,还可以更改给定数组的键。它还保持数组的顺序:https: //stackoverflow.com/a/57622225/10452175

