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,