将CSV解析为数组并搜索该数组中的值
What I want to do is insert a 47kb csv file into an array in the following method.
CSV Structure:
ID;TEXT;INTEGER;VARCHAR(1)
Example: 1;Random Text;0;Q
But I have multiple id's in the CSV file because this is a question and answer table and for each question the answers have the same ID of the question.
What I pretend to do is to import the CSV file into an array, then search that array for Q in VARCHAR field, then count that to have the number of questions(Q = question, A = answer).
Having the number of questions I will select 5 for example, then get the ID of them and get the answers to compare the right/wrong ones.
At this point I only want to import the CSV file and search for the questions...
How do I import the CSV into an array in a way that I could search for some values after importing?
I hope I detailed everything to be understandable.
Thank you.
OS:Linux
Language:PHP 5.4
我想要做的是使用以下方法将47kb csv文件插入数组中。 p> \ n
CSV结构: p>
ID; TEXT; INTEGER; VARCHAR(1) p>
示例:1;随机文本; 0; 问 p>
但我在CSV文件中有多个id,因为这是一个问答表,对于每个问题,答案都有相同的问题ID。 p> \ n
我假装要做的是将CSV文件导入一个数组,然后在VARCHAR字段中搜索该数组中的Q,然后将其计算为具有问题数(Q =问题,A =答案)。 p>
例如,我将选择5个问题,然后获取它们的ID并获得比较正确/错误的答案。 p>
此时我只想导入CSV文件并搜索问题...... p>
如何将CSV导入数组,以便我可以搜索某些值 导入? p>
我希望我详细说明一切都可以理解。 p >
谢谢。 p>
OS:Linux p>
语言:PHP 5.4 p> div>
$csvArray = file('file.csv');
$base = array('Q' => array(), 'A' => array());
foreach ($csvArray as $line) {
$lineArray = explode(';', $line);
//$lineArray[0] - ID
//$lineArray[1] - text
//$lineArray[2] - number
//$lineArray[3] - type
if ($lineArray[3] === 'Q') {
$base['Q'][$lineArray[0]] = array(
'text' => $lineArray[1],
'number' => $lineArray[2]
);
} elseif ($lineArray[3] === 'A') {
$base['A'][$lineArray[0]][] = array(
'text' => $lineArray[1],
'number' => $lineArray[2]
);
}
}
//all questions - $base['Q']
//question item - $base['Q'][ID-question]
//all answers for ID-question - $base['A'][ID-question]