在Symfony中将多维数组导出为CSV文档

时间:2018-04-16 16:00:37

标签: php arrays symfony csv export

有这样一个多维数组(带有n嵌套),来自print_r派生:

Array
(
    [id] => 829
    [name] => name surname
    [info] => Array
        (
            [direct] => 13
            [count] => 492
            [total] => 108
            [asoc] => 4.514
        )

    [children] => Array
        (
            [0] => Array
                (
                    [id] => 530
                    [name] => name surname2
                    [info] => Array
                        (
                            [direct] => 6
                            [count] => 49
                            [asoc] => 3.267
                            [total] => 14
                        )

                    [_children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 628
                                    [name] => name surname3
                                    [info] => Array
                                        (
                                            [direct] => 1
                                            [count] => 1
                                            [asoc] => 1
                                            [total] => 0
                                        )

                                    [_children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 2
                                                    [name] => Name..
                                                    [info] => Array
                                                        (
                                                            [direct] => 0
                                                            [count] => 0
                                                            [asoc] => 0
                                                        )

                                                    [children] => Array
                                                        (
                                                        )
                                             [1] => Array
                                                     (
                                                    [id] => 3
                                                    [name] => Name..S
                                                    [info] => Array
                                                        (
                                                            [direct] => 1
                                                            [count] => 2
                                                            [asoc] => 1
                                                        )

                                                    [children] => Array
                                                        (
                                                        )

我需要将其写入CSV文件,保留结构(树)。底线是我尝试了很多方法如何将其写入文件,但由于某种原因我没有成功(不严格判断,我是新手)。 CSV文档应具有以下结构:

name surname (13, 492, 108,4.514)//в скобках это info из массива                
    name surname2 (6, 49,3.267, 14) 
    name surname3 (1, 1, 1)         
                  name surname4
                  name surname5(1,2,1)   

我将非常感谢有关此事的任何有用信息(或书籍)!谢谢! 我试着这样做:

foreach (new \RecursiveIteratorIterator(new \RecursiveArrayIterator($structureTree)) as $key => $value) {
            fputcsv($file, array($value));
        }

但我收到了这个:

829
name surname
13
492
108
4.514
...
Name..S
1
2
1

更新 我试过这个,但我怎么能做树结构?

public function recursive($array, $file)
    {
        foreach ($array as $key => $item) {
            if (is_array($item) && $key === 'info')
                fputcsv($file, $item);
            elseif (is_array($item)) {
                $this->recursive($item, $file);
                fputcsv($file,array('-'));
            }
            elseif ($key == 'id')
                    continue;
                else
                    fputcsv($file, array($item));
        }
    }

0 个答案:

没有答案