改变array_walk函数中的数组键? [英] changing array keys in array_walk function?

查看:200
本文介绍了改变array_walk函数中的数组键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ piper =| k = F | p = T |Ë= 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);

这给了我

  array(size = 4)
1 =>字符串'f'(长度= 1)
2 =>字符串't'(长度= 1)
3 =>字符串'r'(长度= 1)
4 =>字符串'm'(长度= 1)

我想要的:

  array(size = 4)
k=>字符串'f'(长度= 1)
p=>字符串't'(长度= 1)
e=>字符串'r'(长度= 1)
t=>字符串'm'(长度= 1)

但密钥未改变。是否有任何数组函数,我可以循环一个数组,并更改键和值?

解决方案

据说在 array_walk 的文档(描述回调函数):


只有数组的值可能会被改变;其结构
不能改变,即程序员不能添加,取消设置或重新排列
元素。如果回调不尊重这个要求,这个函数的
行为是未定义的,并且是不可预知的。


这意味着你不能使用 array_walk 来改变迭代数组的键。然而,你可以用它创建一个新的数组:

pre $ code $ $ result = array();
array_walk($ piper,function(& $ value,$ key)use(& $ result){
$ splitted = explode(=,$ value);
$ result [$ splitted [0]] = $ splitted [1];
});
var_dump($ result);






不过,我认为如果是我, '在这里使用正则表达式(而不是爆炸爆炸):

  $ piper =| k = f | p =吨|电子= R | T = M |; 
preg_match_all('#([^ = |] *)=([^ |] *)#',$ piper,$ matches,PREG_PATTERN_ORDER);
$ piper = array_combine($ matches [1],$ matches [2]);
var_dump($ piper);


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?

解决方案

It's said in the documentation of array_walk (describing the callback function):

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_walk to alter the keys of iterated array. You can, however, create a new array with it:

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

这篇关于改变array_walk函数中的数组键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆