读取 CSV 文件并创建具有父子关系的动态 PHP 数组
Read CSV file and create dynamic PHP array having parent-child relationship
我有以下格式的 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 文件可以有任意数量的列,因此我需要一些动态的方法来创建一个父子关系数组。
到目前为止,我已经尝试了很多不同的方法,但是 none 对我有用,因为我必须对每个 CSV $row 索引进行硬编码才能获得 CSV $row 项。
请帮忙,任何帮助将不胜感激,
如果我理解正确的话,最后两项始终是值。剩下的就是按键,但是按键的数量可能会有所不同。
所以我们可以提取值(即最后两项),其余的将是数组键。然后我们可以遍历所有键,或者在这个例子中我选择简单地将它们内爆到一个字符串:
<?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()
:。
我有以下格式的 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 文件可以有任意数量的列,因此我需要一些动态的方法来创建一个父子关系数组。
到目前为止,我已经尝试了很多不同的方法,但是 none 对我有用,因为我必须对每个 CSV $row 索引进行硬编码才能获得 CSV $row 项。
请帮忙,任何帮助将不胜感激,
如果我理解正确的话,最后两项始终是值。剩下的就是按键,但是按键的数量可能会有所不同。
所以我们可以提取值(即最后两项),其余的将是数组键。然后我们可以遍历所有键,或者在这个例子中我选择简单地将它们内爆到一个字符串:
<?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()
: