给出每个索引数组的名称

时间:2016-04-17 06:15:47

标签: php arrays multidimensional-array

所以我得到了一个像这样的多维数组,这是一些计算的结果:

 Array
    (
        [0] => Array
            (
                [0] => 0.0415
                [1] => 0.083
                [2] => 0.083
            )
        [1] => Array
            (
                [0] => 0.0325
                [1] => 0.041
                [2] => 0.025
             )
        [1] => Array
             (
                [0] => 0.076
                [1] => 0.005
                [2] => 0.031
              )
     )

是否可以给出每个索引的名称?我期望的结果是这样的:

 Array
    (
        [A1] => Array
            (
                [C1] => 0.0415
                [C2] => 0.083
                [C3] => 0.083
            )
        [A2] => Array
            (
                [C1] => 0.0325
                [C2] => 0.041
                [C3] => 0.025
             )
        [A3] => Array
             (
                [C1] => 0.076
                [C2] => 0.005
                [C3] => 0.031
              )
     )

1 个答案:

答案 0 :(得分:0)

是的,可以使用foreach循环。

$b=array();
foreach ($a as $k => $v) {
    $new_key='A'.$k;
        $new_array= array();
         foreach ($v as $k2 => $val) {
           $kkk = 'C'.$k2;
           $new_array[$kkk]=$val;
         } 
        $b[$new_key]=$new_array;
}