Jump to content

Recommended Posts

I am importing a text/csv file and instead of importing all of the 'columns' (there are 15 columns on each line), I would like to import column number 1, 4, 15.

 

I have the following code that will import the whole file into an array which works fine but i don't want the whole file in the array becuase I also need to do something like array_unique() where i filter out any duplicates once I have my 3 columns imported into an array.

 

My question is:

 

1.  How do i import the file to only read in the columns i want

 

2. Once I have all the data into an array , how do i get all the unique rows (so in theory the array size could be halved)

 

Any help greatly appreciated

# Open the File.
    if (($handle = fopen($files, "r")) !== FALSE) {
        # Set the parent multidimensional array key to 0.
        $nn = 0;
        while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
            # Count the total keys in the row.
            $c = count($data);
            # Populate the multidimensional array.
            for ($x=0;$x<$c;$x++)
            {
                $csvarray[$nn][$x] = $data[$x];
            }
            $nn++;
        }
        # Close the File.
        fclose($handle);
        
        //rename('imports/' . $files, 'archived/' . $files);
    }

Before the while loop setup an array which contains the columns you want to get from $data.

$targetColumns = array(0, 3, 14); // get data from the 1st, 4th and 15th column

Now replace

            # Count the total keys in the row.
            $c = count($data);
            # Populate the multidimensional array.
            for ($x=0;$x<$c;$x++)
            {
                $csvarray[$nn][$x] = $data[$x];
            }

With

    	$targetData = array();                  // array that hold target data
    	foreach($targetColumns as $column)      // loop throught the targeted columns array
    		$targetData[] = $data[$column]; // get the data from the column

        # Populate the multidimensional array.
        $csvarray[$nn] = $targetData;       // add target data to csvarray

Now $csvarray should contain just your targeted data.

 

For array_unique are you wanting to apply it to the three columns ($targetData) or to the whole of $csvarray?

Edited by Ch0cu3r
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.