CSV 到关联数组
我见过很多关于如何获取 CSV 文件,然后创建一个以标题为键的关联数组的示例。
例如:
Brand,Model,Part,Test
Honda,Civic,123,244
Honda,Civic,135,434
Toyota,Supra,511,664
它将创建一个数组,例如 Array[$num][$key]
,其中 $key
是品牌、型号、零件、测试。
因此,如果我想访问测试值“434”,我必须循环数组中的每个索引,然后忽略任何不是本田的品牌,以及任何不是思域的车型
我需要做的是最直接地访问该值,而不是通过 for 循环遍历每个 $num 索引。我希望能够通过以下方式访问值测试“434”:
Array['Honda']['Civic']['135']
或通过循环遍历本田的每个型号来控制 for 语句...类似于
foreach $model in Array['Honda']
至少我需要能够遍历给定已知品牌的每个模型并访问每个模型的所有相关信息。
编辑:
只是为了确认我正在设置一个示例。我的实际数据具有如下标题:
品牌型号零件价格运输描述脚注
其中我需要访问与零件相关的所有信息(价格、运输、描述、脚注)
I've seen numerous examples on how to take a CSV file and then create an associative array with the headers as the keys.
For example:
Brand,Model,Part,Test
Honda,Civic,123,244
Honda,Civic,135,434
Toyota,Supra,511,664
Where it would create an Array such as Array[$num][$key]
where $key
would be Brand, Model, Part, Test.
So If I wanted to access the test value "434" I would have to loop every index in the array and then ignore any Brands that were not honda, and any models that were not Civic
What I need to do is access the value most directly, instead of running through a for loop going through each $num index. I want to be able to access the value test "434" with:
Array['Honda']['Civic']['135']
or control a for statement with looping through every model Honda has... something like
foreach $model in Array['Honda']
At the very least I need to be able to go through every model given a known Brand and access all the relative info for each.
Edit:
Just to confirm I was setting this up an example. My actually data has headers like:
brand model part price shipping description footnote
Of which I need to access all the information tied to the part (price, shipping,desc, footnote)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
太多长解决方案。我一直发现这是最简单的:
Too many long solutions. I've always found this to be the simplest:
逐行运行 csv 文件,然后插入到数组中,如下所示:
run over the csv file line by line, and insert to array like:
要创建关联列表数组,请使用以下内容:
要按特定属性遍历和过滤,请编写如下函数:
您可以使用
$find = array(Brand=>Honda, Model=>Civic, Part 调用它=>135)
过滤掉搜索到的模型。其他位置数组结构似乎不太可行,除非您只想访问“Test”属性。To create an associative list array use something like:
And to traverse and filter by specific attributes write a function like:
Where you would invoke it with
$find = array(Brand=>Honda, Model=>Civic, Part=>135)
to filter out the searched models. The other positional array structure seems not very workable, unless you only want to access the "Test" attribute.尝试这个简单的算法:
Try this simple algorithm:
使用 fgetcsv() 似乎是完成这项工作最直接、最明智的工具。
csv.csv 内容:
代码:
输出:
资源: http://php.net/manual /en/function.fgetcsv.php
Using
fgetcsv()
seems the most direct and sensible tool for the job.csv.csv contents:
Code:
Output:
Resource: http://php.net/manual/en/function.fgetcsv.php
这是一个通过指定本地文件或 URL 来工作的解决方案。您还可以打开和关闭关联。希望这有帮助。
试试这个用法:
Here is a solutions that will work by specifying a local file or URL. You can also switch the association on and off. Hopefully this helps.
Try this usage:
这是我的解决方案,与其他人所述的类似,但使用带有 fgetcsv 的 while 循环,并使用计数器和 array_combine 将第一行设置为键。
Here is my solution, similar to others stated but uses a while loop with fgetcsv, and uses a counter and array_combine to set the first row as the keys.