Jump to content

importing matrix (csv-file) into array


Reignfire
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi,

 

I have a csv-file with data like this:

 

post-136907-0-39543400-1399368963_thumb.gif

 

I have a mysql database with all the names of the ingredients and a webpage with a searchbox so for example if i search for apple select it and then i search and select blueberry i want to get the corresponding data for apple and blueberry ( -4 in the example ).

 

I know i can read the csv-file with 'fgetcsv' but i'm not sure how to import it into an array so it's easy to use so i can search for ingredients.

 

Does anybody have an idea how to do this in php? 

Link to comment
Share on other sites

  • Solution

 

 

I know i can read the csv-file with 'fgetcsv' but i'm not sure how to import it into an array so it's easy to use so i can search for ingredients.

I'd parse the data into a multidimensional array. So the row heading, will be used as the array key for the array containing the row data. Id assign each item in the row array with the corresponding column heading as the key. Example array structure

Array
(
    //row 1 heading
    [apple] => Array
        (
            // row data
            [apple] => 3        // column 1 value
            [blueberry] => -4   // column 2 value
            [carrot] => -1
            [date] => 0.0002
            [egg] => 0
            [fish] => 0.0003
            [garlic] => 0.006
            [herring] => 1.00
            [jasmin] => 9.62E-06
        )

    //column 2 heading
    [blueberry] => Array
        (
            // row data
            [apple] => 0
            [blueberry] => 0
            [carrot] => 0
            [date] => 0
            [egg] => 0
            [fish] => 0
            [garlic] => 0
            [herring] => 0
            [jasmin] => 0
        )

        ...etc
}

To generate that array structure you'd use

$handle = fopen('test.csv', "r");

// first row of the csv file contains the column headings
$headings = fgetcsv($handle, 0, ",");
$headings = array_filter($headings);

// parse the the values into an array
$data = array();
while (($row = fgetcsv($handle, 0, ",")) !== false)
{
    // remove the row label from the row, use it as the arrat key later
    $row_label = array_shift($row);

    // set the corresponding heading as the key for the values in the row.
    $data[$row_label] = array_combine($headings, $row);
}

Now you can now easily get the intersecting value using $data[ $row_label ][ $column_label ]

echo $data['apple']['blueberry']; // returns -4
Link to comment
Share on other sites

Hi,

 

is there any reason why you want to load the data into an array? Parsing the entire CSV an every single request is pretty much the most cumbersome and inefficient solution you can get, especially when you're dealing with more data than in your example.

 

It would make much more sense to import the data into your database where it belongs. Then you can actually query it and fetch specific information.

Link to comment
Share on other sites

 

is there any reason why you want to load the data into an array?

 

 

The file i got wasn't the final version so i thought that maybe it would be easier to load the data into an array instead of importing it into the database and having to empty and refill the database everytime i got a new file. But the performance was indeed more important so i parsed the file into the database.

 

Thanks for both replies!

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.