在我的上一个question中,创建了一种基于字符串模式Father_son来划分数组的方法,但是现在我需要一种分割新模式的方法:Father_N_son
原件(由http://ideone.com/pmHHR代码生成):
array
'Pagamento' =>
array
'data' => string '' (length=0)
'0_pessoaId' => string '85' (length=2)
'0_valorBruto' => string '890.00' (length=6)
'1_pessoaId' => string '83' (length=2)
'1_valorBruto' => string '20.00' (length=5)现在我需要:
array
'Pagamento' =>
array
'data' => string '' (length=0)
0 =>
array
'pessoaId' => string '85' (length=2)
'valorBruto' => string '890.00' (length=6)
1 =>
array
'pessoaId' => string '83' (length=2)
'valorBruto' => string '20.00' (length=5)谢谢你,塞尔索
发布于 2011-12-08 19:58:22
我的regex解决方案:
static function parserRequest($array = null) {
if (empty($array)) {
$array = $_REQUEST;
}
foreach ($array as $k => $v) {
$name = explode('_', $k);
$newkey = array_shift($name);
$newname = implode('_', $name);
//para o padrão Entidade.n.atributo
if(preg_match("/[0-9]_[a-z]/", strtolower($newname))){
$nameValued = explode('_',$newname);
$newkeyValued = array_shift($name);
$newnameValue = implode('_', $name);
$result[$newkey][$newkeyValued][$newnameValue] = $v;
}else{
//para o padrão Entidade.atributo
$result[$newkey][$newname] = $v;
}
}
return $result;
}https://stackoverflow.com/questions/8420298
复制相似问题