如何转换csv到递归数组在php? [英] How to convert csv to recursive array in php?
本文介绍了如何转换csv到递归数组在php?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将csv转换为多维数组。我已经附加文件,想要像数组一样。
I want to convert csv to multidimensional array. I have attached file and want to array like.
array(
[CANARA BANK] =>
array(
[Banking services] =>
array(
[Customer Identification] =>
array(
[Domestic rates 1] =>
array(
[0] => 'Domestic rates 2'
[1] => 'Domestic rates 1'
)
)
)
)
)
推荐答案
$csvData = file_get_contents('Book2.csv');
$lines = explode("\n", $csvData);
$head = str_getcsv(array_shift($lines));
$array = array();
foreach ($lines as $line) {
$row = array_pad(str_getcsv($line), count($head), '');
$array13[] = array_combine($head, $row);
}
echo "<pre>";
print_r($array13);
Array
(
[0] => Array
(
[Brand] => CANARA BANK
[Product] => Banking services
[Level0] => Customer Identification No
[Levl1] => Domestic rates 1
[Level2] => Domestic rates 2
)
[1] => Array
(
[Brand] =>
[Product] =>
[Level0] =>
[Levl1] =>
[Level2] => Domestic rates 3
)
[2] => Array
(
[Brand] =>
[Product] =>
[Level0] =>
[Levl1] =>
[Level2] =>
)
)
但我想要像下面的数组
array(
[CANARA BANK] =>
array(
[Banking services] =>
array(
[Customer Identification] =>
array(
[Domestic rates 1] =>
array(
[0] => 'Domestic rates 3'
[1] => 'Domestic rates 2'
)
)
)
)
)
这篇关于如何转换csv到递归数组在php?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文