How to concatenate two columns in a csv file in php?


Patrick 62

I have a csv file like this:enter image description here

I want to concatenate the values ​​of the style_color column in this csv file. For example with SCJEG4_1014. I wrote a script that creates the last column with the header "Pictures Names", but in each cell I only have "_".

How can I solve my problem?

<?php

//uploaded xlsx file recovery
$xlsx="C:/wamp64/www/Extract_pictures_Excel/xlsx_files/".date('Y_m_d H-i-s')."_file.xlsx";
move_uploaded_file($_FILES["mon_fichier"]["tmp_name"],$xlsx);

// Excel in CSV
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$excel = PHPExcel_IOFactory::load($xlsx);
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->setDelimiter(";");
$writer->setEnclosure("");
$nomcsv = "C:/wamp64/www/Extract_pictures_Excel/csv/".date('Ymd_His').".csv";
$writer->save($nomcsv);

$delimiter = ";"; 
$csv_data = array();
$row = 1;
if (($handle = fopen($nomcsv, 'r')) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
        $names_pictures = $data[7].'_'.$data[4];  
        $csv_data[] = $data;
        $row++;      
    }
    fclose($handle);
}

$extra_columns = array('Pictures Names' => $names_pictures);
foreach ($csv_data as $i => $data) {
    if ($i == 0) {
        $csv_data[$i] = array_merge($data, array_keys($extra_columns));
    } else {
        $csv_data[$i] = $data = array_merge($data, $extra_columns);
    }
}


if (($handle = fopen($nomcsv, 'w')) !== FALSE) {
    foreach ($csv_data as $data) {
        fputcsv($handle, $data, $delimiter);
    }
    fclose($handle);
}


?>
Nigel Ren

It looks like you've only added details from the last line (since you're only using the value $names_picturesonce ). (IMHO) It's best to add this value to the data at the point where the $csv_dataarray is generated .

while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
    $data['Pictures Names'] = $data[7] . '_' . $data[4];  
    $csv_data[] = $data;
    $row++;      
}

Then you can remove foreach ($csv_data as $i => $data) {the loop

If you have different output files, you can open the output file before the above loop and write the data directly to the output file instead of using $csv_data...

if (($handle = fopen($nomcsv, 'r')) !== FALSE 
        && ($ohandle = fopen($nomcsvOutput, 'w')) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
        $data['Pictures Names'] = $data[7] . '_' . $data[4];  
        fputcsv($ohandle, $data, $delimiter);  
    }
    fclose($handle);
    fclose($ohandle);
}

Related


How to concatenate two columns in a csv file in php?

Patrick 62 I have a csv file like this: I want to concatenate the values of the style_color column in this csv file. For example with SCJEG4_1014. I wrote a script that creates the last column with the header "Pictures Names", but in each cell I only have "_".

How to concatenate two columns in a csv file in php?

Patrick 62 I have a csv file like this: I want to concatenate the values of the style_color column in this csv file. For example with SCJEG4_1014. I wrote a script that creates the last column with the header "Pictures Names", but in each cell I only have "_".

Concatenate columns in CSV file

Keith I have a CSV file that looks like this: Computer, App PC1, Software1 PC1, Software2 PC1, Software3 PC2, Software1 PC2, Software4 PC2, Software5 I want a CSV file like this: Computer, Apps PC1, Software1;Software2;Software3 PC2, Software1;Software4;Softw

Concatenate columns in CSV file

Keith I have a CSV file that looks like this: Computer, App PC1, Software1 PC1, Software2 PC1, Software3 PC2, Software1 PC2, Software4 PC2, Software5 I want a CSV file like this: Computer, Apps PC1, Software1;Software2;Software3 PC2, Software1;Software4;Softw

How to concatenate two columns in mysql in PHP

Arthur I'm trying to put all the names in MySQL into a dropdown list using PHP. I use PDO to connect to MySQL. Currently I can only get the first name, but I want the first name in the dropdown to be first + last name, but I can't connect them. I tried to conn

How to concatenate two columns in mysql in PHP

Arthur I'm trying to put all the names in MySQL into a dropdown list using PHP. I use PDO to connect to MySQL. Currently I can only get the first name, but I want the first name in the dropdown to be first + last name, but I can't connect them. I tried to conn

How to concatenate two columns

user 2009265 I need to concatenate two columns firstnameand lastnameselect names in a crosstab like: select * from crosstab('SELECT concat(u.firstname," ",u.lastname)as name But I get this error: column « » doesn't exist. what should I do? Robert N Try this

Concatenate two columns of a text file

Stanton Arch I have a tsv file like 1 2 3 4 5 ... a b c d e ... x y z j k ... How can I merge two consecutive columns, say second and third, to get 1 2-3 4 5 ... a b-c d e ... x y-z j k ... I need code to

Concatenate two columns of a text file

Stanton Arch I have a tsv file like 1 2 3 4 5 ... a b c d e ... x y z j k ... How can I merge two consecutive columns, say second and third, to get 1 2-3 4 5 ... a b-c d e ... x y-z j k ... I need code to

Concatenate two columns of a text file

Stanton Arch I have a tsv file like 1 2 3 4 5 ... a b c d e ... x y z j k ... How can I merge two consecutive columns, say second and third, to get 1 2-3 4 5 ... a b-c d e ... x y-z j k ... I need code to

Concatenate two columns of a text file

Stanton Arch I have a tsv file like 1 2 3 4 5 ... a b c d e ... x y z j k ... How can I merge two consecutive columns, say second and third, to get 1 2-3 4 5 ... a b-c d e ... x y-z j k ... I need code to

Concatenate two columns of a text file

Stanton Arch I have a tsv file like 1 2 3 4 5 ... a b c d e ... x y z j k ... How can I merge two consecutive columns, say second and third, to get 1 2-3 4 5 ... a b-c d e ... x y-z j k ... I need code to

Concatenate two columns of a text file

Stanton Arch I have a tsv file like 1 2 3 4 5 ... a b c d e ... x y z j k ... How can I merge two consecutive columns, say second and third, to get 1 2-3 4 5 ... a b-c d e ... x y-z j k ... I need code to

Concatenate two columns of a text file

Stanton Arch I have a tsv file like 1 2 3 4 5 ... a b c d e ... x y z j k ... How can I merge two consecutive columns, say second and third, to get 1 2-3 4 5 ... a b-c d e ... x y-z j k ... I need code to

Concatenate two CSV files with two columns

Nora Mahmood I have two csv files, one for blood pressure of a patient and another for heart rate measured for the same patient and same hour, see example below First csv: subject_id hour_id blood_pressure 1 1 96

How to concatenate columns in a CSV file using Pandas in Python

username I have a CSV file that looks like this: # data.csv (this line is not there in the file) Names, Age, Names John, 5, Jane Rian, 29, Rath When I read it through Pandas in Python, I get something like this: import pandas as pd data = pd.read_csv("data.c

How to concatenate columns in a CSV file using Pandas in Python

username I have a CSV file that looks like this: # data.csv (this line is not there in the file) Names, Age, Names John, 5, Jane Rian, 29, Rath When I read it through Pandas in Python, I get something like this: import pandas as pd data = pd.read_csv("data.c

How to concatenate columns in a CSV file using Pandas in Python

username I have a CSV file that looks like this: # data.csv (this line is not there in the file) Names, Age, Names John, 5, Jane Rian, 29, Rath When I read it through Pandas in Python, I get something like this: import pandas as pd data = pd.read_csv("data.c

How to concatenate columns in a CSV file using Pandas in Python

username I have a CSV file that looks like this: # data.csv (this line is not there in the file) Names, Age, Names John, 5, Jane Rian, 29, Rath When I read it through Pandas in Python, I get something like this: import pandas as pd data = pd.read_csv("data.c

How to concatenate columns in a CSV file using Pandas in Python

username I have a CSV file that looks like this: # data.csv (this line is not there in the file) Names, Age, Names John, 5, Jane Rian, 29, Rath When I read it through Pandas in Python, I get something like this: import pandas as pd data = pd.read_csv("data.c

How to concatenate two extracted columns?

dark blue I need to show the total number of days and hours someone has checked in. The code below creates variables in separate columns with no identifiers other than the column headers. I can also create a column that shows the total hours. select id, fname,

How to concatenate two extracted columns?

dark blue I need to show the total number of days and hours someone has checked in. The code below creates variables in separate columns with no identifiers other than the column headers. I can also create a column that shows the total hours. select id, fname,

How to sort csv file by two columns in python?

Lala I have a csv file with 6 columns. I want to sort it by column 2 and then by column 3. My current code is creating a blank file: import csv with open('original.csv', mode='rt') as f, open('sorted.csv', 'w') as final: writer = csv.writer(final, deli

How to link two columns in Excel CSV file

Miguel I need to create a database in Excel which must be saved as a .csv file. my goal is: for each row in column G. If these values are the same, compare it with row L, copying the values in J->H and K->I. About 45.000 records must be completed. Is there a f

How to sort csv file by two columns in python?

Lala I have a csv file with 6 columns. I want to sort it by column 2 and then by column 3. My current code is creating a blank file: import csv with open('original.csv', mode='rt') as f, open('sorted.csv', 'w') as final: writer = csv.writer(final, deli

How to sort csv file with two columns in java?

Yellow How to sort CSV file by two columns? Now I can sort it by one column. I need to sort it by the first two columns. How to do it? Here is the code I use to sort by its first column: import java.io.BufferedReader; import java.io.FileReader; import java.io.

How to sort csv file by two columns in Java?

Joseph Wong How to sort CSV file by two columns? Now I can sort it by one column. I need to sort it by the first two columns. How to do it? Here is the code I use to sort it by its first column: import java.io.BufferedReader; import java.io.FileReader; import

How to sort csv file by two columns in python?

Lala I have a csv file with 6 columns. I want to sort it by column 2 and then by column 3. My current code is creating a blank file: import csv with open('original.csv', mode='rt') as f, open('sorted.csv', 'w') as final: writer = csv.writer(final, deli

How to link two columns in Excel CSV file

Miguel I need to create a database in Excel which must be saved as a .csv file. my goal is: for each row in column G. If these values are the same, compare it with row L, copying the values in J->H and K->I. About 45.000 records must be completed. Is there a f