从未知深度数组创建一个简单的二维数组

时间:2014-06-05 17:47:11

标签: php

我有未知的数组深度,但我想制作简单的二维数组如下,早些时候我认为我的问题没有确切意义所以我编辑了这个,请帮助

private function arrayDepth($a) {
$arr = array();
foreach ($a as $key => $val) {
    if (is_array($val)) {
        $this->arrayDepth($val);
     }
        else  {           
            $arr[] = $a;
        }           
    }       

} 

我当前的数组是

 Array
(
[0] => Array
    (
        [0] => Array
            (
                [0] => 41
                [uid] => 41
                [1] => 16
                [pid] => 16
                [2] => 30
                [oid] => 30
                [3] => 1
                [value] => 1
                [4] => 0
                [optval] => 0
                [5] => 2014-05-26 16:41:31
                [updateDate] => 2014-05-26 16:41:31
            )

        [1] => Array
            (
                [0] => Array
                    (
                        [0] => 42
                        [uid] => 42
                        [1] => 16
                        [pid] => 16
                        [2] => 31
                        [oid] => 31
                        [3] => 1
                        [value] => 1
                        [4] => 0
                        [optval] => 0
                        [5] => 2014-05-26 16:45:49
                        [updateDate] => 2014-05-26 16:45:49
                    )

                [1] => Array
                    (
                        [0] => 44
                        [uid] => 44
                        [1] => 16
                        [pid] => 16
                        [2] => 31
                        [oid] => 31
                        [3] => 1
                        [value] => 1
                        [4] => 0
                        [optval] => 0
                        [5] => 2014-05-26 16:42:01
                        [updateDate] => 2014-05-26 16:42:01
                    )

                [2] => Array
                    (
                        [0] => 47
                        [uid] => 47
                        [1] => 16
                        [pid] => 16
                        [2] => 31
                        [oid] => 31
                        [3] => 1
                        [value] => 1
                        [4] => 0
                        [optval] => 0
                        [5] => 2014-05-26 16:45:19
                        [updateDate] => 2014-05-26 16:45:19
                    )

                [3] => Array
                    (
                        [0] => Array
                            (
                                [0] => 51
                                [uid] => 51
                                [1] => 16
                                [pid] => 16
                                [2] => 32
                                [oid] => 32
                                [3] => 1
                                [value] => 1
                                [4] => 0
                                [optval] => 0
                                [5] => 2014-05-26 16:47:27
                                [updateDate] => 2014-05-26 16:47:27
                            )

                    )

            )

    )

我需要输出像所有数组一样,包含一些值,如

array(
     [0] => Array
            (
                [0] => 41
                [uid] => 41
                [1] => 16
                [pid] => 16
                [2] => 30
                [oid] => 30
                [3] => 1
                [value] => 1
                [4] => 0
                [optval] => 0
                [5] => 2014-05-26 16:41:31
                [updateDate] => 2014-05-26 16:41:31
            )
            [1] => Array
                    (
                        [0] => 42
                        [uid] => 42
                        [1] => 16
                        [pid] => 16
                        [2] => 31
                        [oid] => 31
                        [3] => 1
                        [value] => 1
                        [4] => 0
                        [optval] => 0
                        [5] => 2014-05-26 16:45:49
                        [updateDate] => 2014-05-26 16:45:49
                    )

                [2] => Array
                    (
                        [0] => 44
                        [uid] => 44
                        [1] => 16
                        [pid] => 16
                        [2] => 31
                        [oid] => 31
                        [3] => 1
                        [value] => 1
                        [4] => 0
                        [optval] => 0
                        [5] => 2014-05-26 16:42:01
                        [updateDate] => 2014-05-26 16:42:01
                    )   ...... and so on  .....

1 个答案:

答案 0 :(得分:0)

所以最后我解决了这个问题,如果你们有更好的解决方案请提供

function arrayDepth($a,&$arr) {
        foreach ($a as $key => $val) {
        if(is_array($val))
        if (is_array(@$val[0])) {
            arrayDepth($val,$arr);
        }
        else  {             
            $arr[] = $val;
     }      
      }     
}
arrayDepth($aNonFlat,$arr); 
var_dump($arr);