我有一个来自表单的以下数组,其中包含多语言数据:
Array
(
[en_name] => ...........
[en_description] => ...........
[gr_name] => ...........
[gr_description] => ...........
)
如何将此数组转换为二维,如:
Array
(
[en] => Array
(
[name] => ...........
[description] => ...........
)
[gr] => Array
(
[name] => ...........
[description] => ...........
)
)
答案 0 :(得分:4)
使用此代码:
$finalArr = array();
foreach($arr as $key => $val) {
$tok = explode('_', $key);
$finalArr[$tok[0]][$tok[1]] = $val;
}
答案 1 :(得分:1)
尝试以下
$output = array();
foreach($arr as $val){
$prefix = str_replace("_","",substr($val,0,3));
$ending = substr($val,3,strlen($val));
if(!is_array($output[$prefix]))
$output[$prefix] = array();
array_push($output[$prefix],$ending);
}
答案 2 :(得分:1)
$array3d = array();
foreach($arr as $key => $value) {
$keyArr = explode("_", $key);
$array3d[$keyArr[0]][$keyArr[1]] = $value;
}
如果每个键只有1个下划线,那应该有用。