Search code examples
phparrayscsvtreehierarchy

Read CSV file and create dynamic PHP array having parent-child relationship


I have csv file in the following format:

 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

Expected output array:

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

I want to create a multi dimensional array where one level can be child of another, a like tree hierarchy. The CSV file can have any number of columns, therefore I need some dynamic way to just create an array of parent-child relationship.

So far, I have tried many different approaches but none of them worked for me as I had to hard code each CSV $row index to get CSV $row item.

Please help, any help would be much appreciated,


Solution

  • If I understand you correctly the last two items are always the values. The rest are the keys, but how many keys there are may differ.

    So we can extract the values (i.e. last two items) and the rest will be array keys. We can then loop over all the keys, or in this example I've chosen to simply implode them to one string:

    <?php
    foreach ( $row as $rowArr ) {
        $values = array_slice( $rowArr, - 2 );
        $keys   = array_slice( $rowArr, 0, - 2 );
    
        $keysString = "['" . implode( "'], ['", $keys ) . "']";
    
        $result{$keysString} = $values;
    }
    

    Edit

    Upon rereading your question I think it's only the last item that is the value. I'm a bit confused by the value field though.

    If you only want the last value you might as well do:

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

    So now we have an array of keys: $rowArr. If you don't want to use the implode() that I provided in my example: Google is your friend.