3

我有以下数组,来自一个表单,具有这样的多语言数据:

Array
(
    [en_name] => ...........
    [en_description] => ...........
    [gr_name] => ...........
    [gr_description] => ...........
)

如何将此数组转换为二维,如:

Array
(
    [en] => Array
        (
            [name] => ...........
            [description] => ...........
        )

    [gr] => Array
        (
            [name] => ...........
            [description] => ...........
        )
)
4

3 回答 3

4

使用此代码:

$finalArr = array();
foreach($arr as $key => $val) {
   $tok = explode('_', $key);
   $finalArr[$tok[0]][$tok[1]] = $val;
}
于 2013-02-11T15:38:45.500 回答
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);    
        }
于 2013-02-11T15:35:44.640 回答
1
$array3d = array();
foreach($arr as $key => $value) {
    $keyArr = explode("_", $key);
    $array3d[$keyArr[0]][$keyArr[1]] = $value;
}

如果每个键只有 1 个下划线,那应该可以工作。

于 2013-02-11T15:40:09.780 回答