【发布时间】:2017-04-13 04:48:55
【问题描述】:
我有以下格式的 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 $row 索引进行硬编码才能获得 CSV $row 项目。
请帮助,任何帮助将不胜感激,
【问题讨论】:
标签: php arrays csv tree hierarchy