0

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,

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes only the last item is the value. Also, I gave a try to your code above. it helps to some extend but how can i create with an array keys instead of the string you have in above code? Will i need to run loop inside loop to to reach till the last level? can you help please?
From your code, i would like to achieve something like this. $result[$keys[0]][$keys[1] ][$keys[2] ][$keys[3] ] = $values; but in a dynamic way instead of $keys[0], $keys[1] ..... Which will print me an array in my required format. any ideas? :-)
Yep. I've edited my answer and referenced an answer where they loop over keys.
Thanks, with few changes i was able to achieve my required result. But, your answer put me on the right path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.