使用PHP按列搜索CSV吗? [英] Using PHP to search CSV by Column?
问题描述
按列搜索CSV文件时遇到麻烦.例如,我有以下CSV文件:
Having trouble searching CSV file by Column. For example, I have the following CSV file:
名称,具有IPHONE,具有ANDROID
NAME, HAS AN IPHONE, HAS ANDROID
鲍勃,是的,不,
fred,不,是
如何使用php在第2列中搜索是"值,然后仅返回带有是"的行.在第二列结果中?
How could I search column 2 for a 'yes' value using php, then return only the rows with "yes" in second column results?
推荐答案
假设您已经已将csv文件解析为数组.
array_keys 函数的第二个参数是 mixed $ search_value
Assuming you have already parsed the csv file into an array.
The second parameter of array_keys function is mixed $search_value
如果指定,则仅返回包含这些值的键.
If specified, then only keys containing these values are returned.
要搜索特定的列,请使用 array_column :>
To search a specific column, use array_column:
$res = array_keys(array_column($csv, 1), "yes");
请参见在eval.in进行测试;这将返回第二列中所有是"匹配项的键.
See test at eval.in; This would return keys of of all "yes" matches in the second column.
这篇关于使用PHP按列搜索CSV吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!