0

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

  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
        )
)

寻找任何想法如何进行......

4

1 回答 1

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);

你可以在这里看到一个演示

于 2013-01-07T06:22:16.903 回答