寻找格式化一个数组的想法?

时间:2013-01-07 06:00:36

标签: php arrays codeigniter

我有一个三维数组,如下所示。

  Array
(
[d1] => Array
    (
        [c1] => Array
            (
                [t1] => Array
                    (
                        [a1] => 1
                        [a2] => 2
                        [a3] => 1
                        [a4] => 1
                        [a5] => 1
                        [a6] => 1
                        [a7] => 1
                    )

                [t2] => Array
                    (
                        [a1] => 0
                        [a2] => 1
                        [a3] => 0
                        [a4] => 1
                        [a5] => 2
                        [a6] => 0
                        [a7] => 1                            
                    )

                [t3] => Array
                    (
                        [a1] => 1
                        [a2] => 3
                        [a3] => 2
                        [a4] => 4
                        [a5] => 2
                        [a6] => 2
                        [a7] => 3                            
                    )
            )

        [c2] => Array
            (
                [t1] => Array
                    (
                        [a1] => 1
                        [a2] => 2
                        [a3] => 1
                        [a4] => 2
                        [a5] => 2
                        [a6] => 1
                        [a7] => 2                            
                    )

                [t2] => Array
                    (
                        [a1] => 0
                        [a2] => 1
                        [a3] => 0
                        [a4] => 1
                        [a5] => 0
                        [a6] => 0
                        [a7] => 0                           
                    )

                [t3] => Array
                    (
                        [a1] => 1
                        [a2] => 1
                        [a3] => 1
                        [a4] => 1
                        [a5] => 2
                        [a6] => 1
                        [a7] => 2                            
                    )
            )

    )
[d2] => Array
    (
        [c1] => Array
            (
                [t1] => Array
                    (
                        [a1] => 1
                        [a2] => 2
                        [a3] => 1
                        [a4] => 1
                        [a5] => 1
                        [a6] => 1
                        [a7] => 1
                    )

                [t2] => Array
                    (
                        [a1] => 0
                        [a2] => 1
                        [a3] => 0
                        [a4] => 1
                        [a5] => 2
                        [a6] => 0
                        [a7] => 1                            
                    )

                [t3] => Array
                    (
                        [a1] => 1
                        [a2] => 3
                        [a3] => 2
                        [a4] => 4
                        [a5] => 2
                        [a6] => 2
                        [a7] => 3                            
                    )
            )

        [c2] => Array
            (
                [t1] => Array
                    (
                        [a1] => 1
                        [a2] => 2
                        [a3] => 1
                        [a4] => 2
                        [a5] => 2
                        [a6] => 1
                        [a7] => 2                            
                    )

                [t2] => Array
                    (
                        [a1] => 0
                        [a2] => 1
                        [a3] => 0
                        [a4] => 1
                        [a5] => 0
                        [a6] => 0
                        [a7] => 0                           
                    )

                [t3] => Array
                    (
                        [a1] => 1
                        [a2] => 1
                        [a3] => 1
                        [a4] => 1
                        [a5] => 2
                        [a6] => 1
                        [a7] => 2                            
                    )
            )

    )

)

这是数组的PHP代码:

$data = array(
  'd1' => array(
    'c1' => array(
      't1' => array(
        'a1' => 1,
        'a2' => 2,
        'a3' => 1,
        'a4' => 1,
        'a5' => 1,
        'a6' => 1,
        'a7' => 1,
      ),
      't2' => array(
        'a1' => 0,
        'a2' => 1,
        'a3' => 0,
        'a4' => 1,
        'a5' => 2,
        'a6' => 0,
        'a7' => 1,
      ),
      't3' => array(
        'a1' => 1,
        'a2' => 3,
        'a3' => 2,
        'a4' => 4,
        'a5' => 2,
        'a6' => 2,
        'a7' => 3,
      ),
    ),
    'c2' => array(
      't1' => array(
        'a1' => 1,
        'a2' => 2,
        'a3' => 1,
        'a4' => 2,
        'a5' => 2,
        'a6' => 1,
        'a7' => 2,
      ),
      't2' => array(
        'a1' => 0,
        'a2' => 1,
        'a3' => 0,
        'a4' => 1,
        'a5' => 0,
        'a6' => 0,
        'a7' => 0,
      ),
      't3' => array(
        'a1' => 1,
        'a2' => 1,
        'a3' => 1,
        'a4' => 1,
        'a5' => 2,
        'a6' => 1,
        'a7' => 2,
      ),
    ),
  ),
  'd2' => array(
    'c1' => array(
      't1' => array(
        'a1' => 1,
        'a2' => 2,
        'a3' => 1,
        'a4' => 1,
        'a5' => 1,
        'a6' => 1,
        'a7' => 1,
      ),
      't2' => array(
        'a1' => 0,
        'a2' => 1,
        'a3' => 0,
        'a4' => 1,
        'a5' => 2,
        'a6' => 0,
        'a7' => 1,
      ),
      't3' => array(
        'a1' => 1,
        'a2' => 3,
        'a3' => 2,
        'a4' => 4,
        'a5' => 2,
        'a6' => 2,
        'a7' => 3,
      ),
    ),
    'c2' => array(
      't1' => array(
        'a1' => 1,
        'a2' => 2,
        'a3' => 1,
        'a4' => 2,
        'a5' => 2,
        'a6' => 1,
        'a7' => 2,
      ),
      't2' => array(
        'a1' => 0,
        'a2' => 1,
        'a3' => 0,
        'a4' => 1,
        'a5' => 0,
        'a6' => 0,
        'a7' => 0,
      ),
      't3' => array(
        'a1' => 1,
        'a2' => 1,
        'a3' => 1,
        'a4' => 1,
        'a5' => 2,
        'a6' => 1,
        'a7' => 2,
      ),
    ),
  ),
);

现在我想将这个数组拆分成几个数组,并将这些数组放入一个数组中,如下所示。

   Array
(
[0] => Array 
        (
            [d1] => d1
            [c1] => c1
            [t1] => t1
            [a1] => 1
            [a2] => 2
            [a3] => 1
            [a4] => 1
            [a5] => 1
            [a6] => 1
            [a7] => 1
        )
[1] => Array 
        (
            [d1] => d1
            [c1] => c1
            [t1] => t2
            [a1] => 0
            [a2] => 1
            [a3] => 0
            [a4] => 1
            [a5] => 2
            [a6] => 0
            [a7] => 1
        )
[2] => Array 
        (
            [d1] => d1
            [c1] => c1
            [t1] => t3
            [a1] => 1
            [a2] => 3
            [a3] => 2
            [a4] => 4
            [a5] => 2
            [a6] => 2
            [a7] => 3
        )
[3] => Array 
        (
            [d1] => d1
            [c1] => c2
            [t1] => t1
            [a1] => 1
            [a2] => 2
            [a3] => 1
            [a4] => 2
            [a5] => 2
            [a6] => 1
            [a7] => 2
        )
[4] => Array 
        (
            [d1] => d1
            [c1] => c2
            [t1] => t2
            [a1] => 0
            [a2] => 1
            [a3] => 0
            [a4] => 1
            [a5] => 0
            [a6] => 0
            [a7] => 0
        )
[5] => Array 
        (
            [d1] => d1
            [c1] => c2
            [t1] => t3
            [a1] => 1
            [a2] => 1
            [a3] => 1
            [a4] => 1
            [a5] => 2
            [a6] => 1
            [a7] => 2
        )
[6] => Array 
        (
            [d1] => d1
            [c1] => c1
            [t1] => t1
            [a1] => 1
            [a2] => 2
            [a3] => 1
            [a4] => 1
            [a5] => 1
            [a6] => 1
            [a7] => 1
        )
[1] => Array 
        (
            [d1] => d2
            [c1] => c1
            [t1] => t2
            [a1] => 0
            [a2] => 1
            [a3] => 0
            [a4] => 1
            [a5] => 2
            [a6] => 0
            [a7] => 1
        )
[7] => Array 
        (
            [d1] => d2
            [c1] => c1
            [t1] => t3
            [a1] => 1
            [a2] => 3
            [a3] => 2
            [a4] => 4
            [a5] => 2
            [a6] => 2
            [a7] => 3
        )
[8] => Array 
        (
            [d1] => d2
            [c1] => c2
            [t1] => t1
            [a1] => 1
            [a2] => 2
            [a3] => 1
            [a4] => 2
            [a5] => 2
            [a6] => 1
            [a7] => 2
        )
[9] => Array 
        (
            [d1] => d2
            [c1] => c2
            [t1] => t2
            [a1] => 0
            [a2] => 1
            [a3] => 0
            [a4] => 1
            [a5] => 0
            [a6] => 0
            [a7] => 0
        )
[10] => Array 
        (
            [d1] => d2
            [c1] => c2
            [t1] => t3
            [a1] => 1
            [a2] => 1
            [a3] => 1
            [a4] => 1
            [a5] => 2
            [a6] => 1
            [a7] => 2
        )
)

寻找任何想法如何继续......

1 个答案:

答案 0 :(得分:4)

假设$data是您原来的多维数组:

$final = array();
foreach($data as $di => $dv){
    foreach($dv as $ci => $cv){
        foreach($cv as $ti => $tv){
            $final[] = array_merge(array("d" => $di, "c" => $ci, "t" => $ti), $tv);
        }
    }
}
var_dump($final);

您可以看到演示here