0

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

Fred
  • 313
  • 1
  • 2
  • 11
  • What OS (e.g. Windows, Linux, ...), RDBMS (e.g. Sqlite, Oracle, MS-SQL Server) and, maybe, language (e.g. C, bash, Python, Tcl, Perl) do you want to do this in? That kinda affects the answer :) – Emmet Mar 16 '14 at 23:12
  • Oh sorry. No MySQL in this only PHP and the server is running in Linux. – Fred Mar 16 '14 at 23:15
  • Have you tried *MySQL*'s `LOAD DATA INFILE`? Is [this post](http://stackoverflow.com/questions/3635166/how-to-import-csv-file-to-mysql-table) any help? – Emmet Mar 16 '14 at 23:21
  • You have outlined a task to be completed, but failed to ask a question. Could you rephrase, such that a suitable question is being asked? – salathe Mar 16 '14 at 23:24
  • Once again, I'm sorry, post edited. Question: How do I import the CSV into an array in a way that I could search for some values after importing? – Fred Mar 16 '14 at 23:26
  • Thanks to you for helping man :) – Fred Mar 16 '14 at 23:28
  • @Emmet I don't want to use MySQL server in this process. Thanks for helping. – Fred Mar 16 '14 at 23:30
  • I can think of ways of doing this that are trivial in shell and Python, but my PHP is very rusty (PHP3 was fairly new last time I did much PHP programming). [Googling “PHP read csv”](https://www.google.com/search?q=php+read+csv) turns up a bunch of examples that look like they could be modified fairly easily. – Emmet Mar 16 '14 at 23:45
  • @Emmet Thanks for the answer. Yes, I tried to modify some but i didn't get any good results, there is always something that blocks me for searching inside array. This week I will be quite busy, but wednesday I will try to come up with some solution. If I put something working in the code I will post it here ASAP. – Fred Mar 16 '14 at 23:51

1 Answers1

1
$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]
Sergey Krivov
  • 372
  • 1
  • 4
  • The array have all the data. I will continue developing and leave here the complete code. – Fred Mar 18 '14 at 22:40
  • How can I print all the questions? I tried echoing $base['Q'] but it appear only array... – Fred Mar 18 '14 at 22:48