读取CSV文件并创建具有父子关系的动态PHP数组

时间:2016-11-29 09:04:57

标签: php arrays csv tree hierarchy

我有以下格式的csv文件:

 Field1, Field2, Field3, Field4, Field5

 Live Animals,  LIVE ANIMALS, INDIA,Value,Thousands

 Live Animals,  LIVE ANIMALS, LEBANON,Value,Millions

 Live Animals,  MEAT,INDIA,Value,Thousands

 Live Animals,  MEAT,INDIA,Value,Millions   

 Live Animals,  FISH,INDIA,Value,Thousands  

 Live Animals,  CRUSTACEANS, LEBANON,Value,Millions 

 Live Animals,  DAIRY PRODUCE , INDIA,Value,Thousands   

 Live Animals,  DAIRY PRODUCE , LEBANON,Value,Millions  

 Live Animals,  PRODUCTS OF ANIMAL ORIGIN,INDONESIA,Value,Thousands 

 Live Animals,  PRODUCTS OF ANIMAL ORIGIN,INDONESIA,Value,Millions

 Plant Products, LIVE TREES AND PLANTS,INDONESIA,Value,Thousands    

 Plant Products, LIVE TREES AND PLANTS,INDONESIA,Value,Millions 

 Plant Products, EDIBLE VEGETABLES,USA,Value,Thousands  

 Plant Products, EDIBLE VEGETABLES,USA,Value,Millions   

 Plant Products, EDIBLE FRUIT,UAE,Value,Thousands

 Plant Products, EDIBLE FRUIT,UAE,Value,Millions    

 Plant Products, COFFEE,BAHRAIN,Value,Thousands

 Plant Products, CEREALS,BAHRAIN,Value,Thousands

预期的输出数组

Array
(
    [Live Animals] => Array
        (
            [LIVEANIMALS] => Array
                (
                    [INDIA] => Array
                        (
                            [Value] => Array
                                (
                                    [0] => Thousands
                                    [1] => Millions
                                )
                        )
                    [LEBANON] => Array
                        (
                            [Value] => Array
                                (
                                    [0] => Thousands
                                    [1] => Millions
                                )
                        )
                )

            [MEAT] => Array
                (
                    [INDIA] => Array
                        (
                            [Value] => Array
                                (
                                    [0] => Thousands
                                    [1] => Millions
                                )
                        )
                )

            [FISH] => Array
                (
                    [INDIA] => Array
                        (
                            [Value] => Array
                                (
                                    [0] => Thousands
                                )
                        )
                )
        )
    .... and so on. 
)

我想创建一个多维数组,其中一个级别可以是另一个级别的子级,类似于树级别。 CSV文件可以包含任意数量的列,因此我需要一些动态方法来创建一个父子关系数组。

到目前为止,我已经尝试了许多不同的方法,但它们都没有为我工作,因为我必须对每个CSV $行索引进行硬编码才能获得CSV $ row项。

请帮助,非常感谢任何帮助,

1 个答案:

答案 0 :(得分:0)

如果我理解正确,最后两项始终是值。其余的是键,但有多少键可能不同。

因此我们可以提取值(即最后两项),其余的将是数组键。然后我们可以循环遍历所有键,或者在这个示例中,我选择将它们简单地内插到一个字符串中:

<?php
foreach ( $row as $rowArr ) {
    $values = array_slice( $rowArr, - 2 );
    $keys   = array_slice( $rowArr, 0, - 2 );

    $keysString = "['" . implode( "'], ['", $keys ) . "']";

    $result{$keysString} = $values;
}

修改

在重新阅读您的问题时,我认为它只是最后一项是值。我对 value 字段感到有些困惑。

如果您只想要最后一个值,那么:

$value = array_pop($rowArr);
$keys = $rowArr;

现在我们有一系列密钥:$rowArr。如果您不想使用我在示例中提供的implode()Google is your friend