replace rows from one csv file to another csv where id of is the samePHP?
I have one excel orginal.csv file
ID Name Price
1 Xblue 12
2 Yblue 32
3 Zblue 52
And another copy.csv file
ID Name Price
1 Xblue 89
2 Yblue 43
3 Zblue 45
I want to replace rows from orginal.csv to copy.csv where ID is the same.
Can I do this manually or maybe somehow using PHP?
I search for some options on the internet, but I only found getcsv and readcsv functions that can't help me in this case. Cause this is something like updating CSV file.
php csv
add a comment |
I have one excel orginal.csv file
ID Name Price
1 Xblue 12
2 Yblue 32
3 Zblue 52
And another copy.csv file
ID Name Price
1 Xblue 89
2 Yblue 43
3 Zblue 45
I want to replace rows from orginal.csv to copy.csv where ID is the same.
Can I do this manually or maybe somehow using PHP?
I search for some options on the internet, but I only found getcsv and readcsv functions that can't help me in this case. Cause this is something like updating CSV file.
php csv
You need to get CSV data from both the files then you can use a loop to update data.
– Sayed Mohd Ali
Nov 14 '18 at 14:29
do you have some code example?
– user9819807
Nov 14 '18 at 14:31
add a comment |
I have one excel orginal.csv file
ID Name Price
1 Xblue 12
2 Yblue 32
3 Zblue 52
And another copy.csv file
ID Name Price
1 Xblue 89
2 Yblue 43
3 Zblue 45
I want to replace rows from orginal.csv to copy.csv where ID is the same.
Can I do this manually or maybe somehow using PHP?
I search for some options on the internet, but I only found getcsv and readcsv functions that can't help me in this case. Cause this is something like updating CSV file.
php csv
I have one excel orginal.csv file
ID Name Price
1 Xblue 12
2 Yblue 32
3 Zblue 52
And another copy.csv file
ID Name Price
1 Xblue 89
2 Yblue 43
3 Zblue 45
I want to replace rows from orginal.csv to copy.csv where ID is the same.
Can I do this manually or maybe somehow using PHP?
I search for some options on the internet, but I only found getcsv and readcsv functions that can't help me in this case. Cause this is something like updating CSV file.
php csv
php csv
edited Nov 14 '18 at 14:42
George Appleton
611427
611427
asked Nov 14 '18 at 14:27
user9819807
You need to get CSV data from both the files then you can use a loop to update data.
– Sayed Mohd Ali
Nov 14 '18 at 14:29
do you have some code example?
– user9819807
Nov 14 '18 at 14:31
add a comment |
You need to get CSV data from both the files then you can use a loop to update data.
– Sayed Mohd Ali
Nov 14 '18 at 14:29
do you have some code example?
– user9819807
Nov 14 '18 at 14:31
You need to get CSV data from both the files then you can use a loop to update data.
– Sayed Mohd Ali
Nov 14 '18 at 14:29
You need to get CSV data from both the files then you can use a loop to update data.
– Sayed Mohd Ali
Nov 14 '18 at 14:29
do you have some code example?
– user9819807
Nov 14 '18 at 14:31
do you have some code example?
– user9819807
Nov 14 '18 at 14:31
add a comment |
2 Answers
2
active
oldest
votes
It may end up in request timeout in PHP because it requires so many loops to do it. If someone can reduce the time complexity of this program then it will work. if it even works it will take a lot of time to do it.
while(! feof($f_pointer)){ //open old csv to update loop1
$ar=fgetcsv($f_pointer); // getting first row
for($i=0;$i<count($ar);$i++){ //loop2 first row array
$f_pointer2=fopen("new.csv","r"); open new csv to get data
while(! feof($f_pointer2)){ // loop3 to find ID in new csv
$ar2=fgetcsv($f_pointer2); //getting each row in array
for($j=0;$j<count($ar2);$j++){ //loop4 to compare id of old csv to new csv and update data
if($ar[i] == $ar2[j]){
foreach ($ar2 as $fields) { //loop5
fputcsv($f_pointer, $fields);
}
}
}
}
}
}
?>
can somehow made condition to replace rows who have same IDS?
– user9819807
Nov 14 '18 at 14:58
add a comment |
I've created a little soulution. If order is important you don't have to index the array and loop through the copied array.
<?php
if(file_exists('output.csv'))
{
unlink('output.csv');
}
function fputcsv_eol($handle, $array, $delimiter = ',', $enclosure = '"', $eol = "n") {
$return = fputcsv($handle, $array, $delimiter, $enclosure);
if($return !== FALSE && "n" != $eol && 0 === fseek($handle, -1, SEEK_CUR)) {
fwrite($handle, $eol);
}
return $return;
}
function scanFile($sFilename, $iIndexColumn)
{
$rFile = fopen($sFilename, 'r');
$aData = array();
while(($aLine = fgetcsv($rFile)) !== false)
{
$aData[$aLine[$iIndexColumn]] = $aLine;
}
fclose($rFile);
return $aData;
}
$iIndexColumn = 0;
$iValueColum = 2;
$aOriginalData = scanFile('original.csv', 0);
$aCopyData = scanFile('copy.csv', 0);
foreach($aOriginalData as $iID => $aOriginalDatum)
{
if(array_key_exists($iID, $aCopyData))
{
$aCopyData[$iID] = $aOriginalDatum;
}
}
$rFile = fopen('output.csv', 'w');
foreach($aCopyData as $aCopyDatum)
{
fputcsv_eol($rFile, $aCopyDatum, ',', '"',"rn");
}
fclose($rFile);
fputcsv() expects parameter 2 to be array, boolean given
– user9819807
Nov 14 '18 at 19:44
I'm only mobile at the moment. I think I've fixed it. I'll come back tomorrow to test it probably.
– finder2
Nov 14 '18 at 19:48
ok.................
– user9819807
Nov 15 '18 at 13:19
@doki the example files (changed the separator in the file to ",") are correctly interpreted
– finder2
Nov 15 '18 at 14:23
this code is still not good, it put all values in one row horizontally?
– user9819807
Nov 15 '18 at 14:37
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302496%2freplace-rows-from-one-csv-file-to-another-csv-where-id-of-is-the-samephp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It may end up in request timeout in PHP because it requires so many loops to do it. If someone can reduce the time complexity of this program then it will work. if it even works it will take a lot of time to do it.
while(! feof($f_pointer)){ //open old csv to update loop1
$ar=fgetcsv($f_pointer); // getting first row
for($i=0;$i<count($ar);$i++){ //loop2 first row array
$f_pointer2=fopen("new.csv","r"); open new csv to get data
while(! feof($f_pointer2)){ // loop3 to find ID in new csv
$ar2=fgetcsv($f_pointer2); //getting each row in array
for($j=0;$j<count($ar2);$j++){ //loop4 to compare id of old csv to new csv and update data
if($ar[i] == $ar2[j]){
foreach ($ar2 as $fields) { //loop5
fputcsv($f_pointer, $fields);
}
}
}
}
}
}
?>
can somehow made condition to replace rows who have same IDS?
– user9819807
Nov 14 '18 at 14:58
add a comment |
It may end up in request timeout in PHP because it requires so many loops to do it. If someone can reduce the time complexity of this program then it will work. if it even works it will take a lot of time to do it.
while(! feof($f_pointer)){ //open old csv to update loop1
$ar=fgetcsv($f_pointer); // getting first row
for($i=0;$i<count($ar);$i++){ //loop2 first row array
$f_pointer2=fopen("new.csv","r"); open new csv to get data
while(! feof($f_pointer2)){ // loop3 to find ID in new csv
$ar2=fgetcsv($f_pointer2); //getting each row in array
for($j=0;$j<count($ar2);$j++){ //loop4 to compare id of old csv to new csv and update data
if($ar[i] == $ar2[j]){
foreach ($ar2 as $fields) { //loop5
fputcsv($f_pointer, $fields);
}
}
}
}
}
}
?>
can somehow made condition to replace rows who have same IDS?
– user9819807
Nov 14 '18 at 14:58
add a comment |
It may end up in request timeout in PHP because it requires so many loops to do it. If someone can reduce the time complexity of this program then it will work. if it even works it will take a lot of time to do it.
while(! feof($f_pointer)){ //open old csv to update loop1
$ar=fgetcsv($f_pointer); // getting first row
for($i=0;$i<count($ar);$i++){ //loop2 first row array
$f_pointer2=fopen("new.csv","r"); open new csv to get data
while(! feof($f_pointer2)){ // loop3 to find ID in new csv
$ar2=fgetcsv($f_pointer2); //getting each row in array
for($j=0;$j<count($ar2);$j++){ //loop4 to compare id of old csv to new csv and update data
if($ar[i] == $ar2[j]){
foreach ($ar2 as $fields) { //loop5
fputcsv($f_pointer, $fields);
}
}
}
}
}
}
?>
It may end up in request timeout in PHP because it requires so many loops to do it. If someone can reduce the time complexity of this program then it will work. if it even works it will take a lot of time to do it.
while(! feof($f_pointer)){ //open old csv to update loop1
$ar=fgetcsv($f_pointer); // getting first row
for($i=0;$i<count($ar);$i++){ //loop2 first row array
$f_pointer2=fopen("new.csv","r"); open new csv to get data
while(! feof($f_pointer2)){ // loop3 to find ID in new csv
$ar2=fgetcsv($f_pointer2); //getting each row in array
for($j=0;$j<count($ar2);$j++){ //loop4 to compare id of old csv to new csv and update data
if($ar[i] == $ar2[j]){
foreach ($ar2 as $fields) { //loop5
fputcsv($f_pointer, $fields);
}
}
}
}
}
}
?>
edited Nov 15 '18 at 14:41
answered Nov 14 '18 at 14:45
Sayed Mohd AliSayed Mohd Ali
1,1152419
1,1152419
can somehow made condition to replace rows who have same IDS?
– user9819807
Nov 14 '18 at 14:58
add a comment |
can somehow made condition to replace rows who have same IDS?
– user9819807
Nov 14 '18 at 14:58
can somehow made condition to replace rows who have same IDS?
– user9819807
Nov 14 '18 at 14:58
can somehow made condition to replace rows who have same IDS?
– user9819807
Nov 14 '18 at 14:58
add a comment |
I've created a little soulution. If order is important you don't have to index the array and loop through the copied array.
<?php
if(file_exists('output.csv'))
{
unlink('output.csv');
}
function fputcsv_eol($handle, $array, $delimiter = ',', $enclosure = '"', $eol = "n") {
$return = fputcsv($handle, $array, $delimiter, $enclosure);
if($return !== FALSE && "n" != $eol && 0 === fseek($handle, -1, SEEK_CUR)) {
fwrite($handle, $eol);
}
return $return;
}
function scanFile($sFilename, $iIndexColumn)
{
$rFile = fopen($sFilename, 'r');
$aData = array();
while(($aLine = fgetcsv($rFile)) !== false)
{
$aData[$aLine[$iIndexColumn]] = $aLine;
}
fclose($rFile);
return $aData;
}
$iIndexColumn = 0;
$iValueColum = 2;
$aOriginalData = scanFile('original.csv', 0);
$aCopyData = scanFile('copy.csv', 0);
foreach($aOriginalData as $iID => $aOriginalDatum)
{
if(array_key_exists($iID, $aCopyData))
{
$aCopyData[$iID] = $aOriginalDatum;
}
}
$rFile = fopen('output.csv', 'w');
foreach($aCopyData as $aCopyDatum)
{
fputcsv_eol($rFile, $aCopyDatum, ',', '"',"rn");
}
fclose($rFile);
fputcsv() expects parameter 2 to be array, boolean given
– user9819807
Nov 14 '18 at 19:44
I'm only mobile at the moment. I think I've fixed it. I'll come back tomorrow to test it probably.
– finder2
Nov 14 '18 at 19:48
ok.................
– user9819807
Nov 15 '18 at 13:19
@doki the example files (changed the separator in the file to ",") are correctly interpreted
– finder2
Nov 15 '18 at 14:23
this code is still not good, it put all values in one row horizontally?
– user9819807
Nov 15 '18 at 14:37
|
show 3 more comments
I've created a little soulution. If order is important you don't have to index the array and loop through the copied array.
<?php
if(file_exists('output.csv'))
{
unlink('output.csv');
}
function fputcsv_eol($handle, $array, $delimiter = ',', $enclosure = '"', $eol = "n") {
$return = fputcsv($handle, $array, $delimiter, $enclosure);
if($return !== FALSE && "n" != $eol && 0 === fseek($handle, -1, SEEK_CUR)) {
fwrite($handle, $eol);
}
return $return;
}
function scanFile($sFilename, $iIndexColumn)
{
$rFile = fopen($sFilename, 'r');
$aData = array();
while(($aLine = fgetcsv($rFile)) !== false)
{
$aData[$aLine[$iIndexColumn]] = $aLine;
}
fclose($rFile);
return $aData;
}
$iIndexColumn = 0;
$iValueColum = 2;
$aOriginalData = scanFile('original.csv', 0);
$aCopyData = scanFile('copy.csv', 0);
foreach($aOriginalData as $iID => $aOriginalDatum)
{
if(array_key_exists($iID, $aCopyData))
{
$aCopyData[$iID] = $aOriginalDatum;
}
}
$rFile = fopen('output.csv', 'w');
foreach($aCopyData as $aCopyDatum)
{
fputcsv_eol($rFile, $aCopyDatum, ',', '"',"rn");
}
fclose($rFile);
fputcsv() expects parameter 2 to be array, boolean given
– user9819807
Nov 14 '18 at 19:44
I'm only mobile at the moment. I think I've fixed it. I'll come back tomorrow to test it probably.
– finder2
Nov 14 '18 at 19:48
ok.................
– user9819807
Nov 15 '18 at 13:19
@doki the example files (changed the separator in the file to ",") are correctly interpreted
– finder2
Nov 15 '18 at 14:23
this code is still not good, it put all values in one row horizontally?
– user9819807
Nov 15 '18 at 14:37
|
show 3 more comments
I've created a little soulution. If order is important you don't have to index the array and loop through the copied array.
<?php
if(file_exists('output.csv'))
{
unlink('output.csv');
}
function fputcsv_eol($handle, $array, $delimiter = ',', $enclosure = '"', $eol = "n") {
$return = fputcsv($handle, $array, $delimiter, $enclosure);
if($return !== FALSE && "n" != $eol && 0 === fseek($handle, -1, SEEK_CUR)) {
fwrite($handle, $eol);
}
return $return;
}
function scanFile($sFilename, $iIndexColumn)
{
$rFile = fopen($sFilename, 'r');
$aData = array();
while(($aLine = fgetcsv($rFile)) !== false)
{
$aData[$aLine[$iIndexColumn]] = $aLine;
}
fclose($rFile);
return $aData;
}
$iIndexColumn = 0;
$iValueColum = 2;
$aOriginalData = scanFile('original.csv', 0);
$aCopyData = scanFile('copy.csv', 0);
foreach($aOriginalData as $iID => $aOriginalDatum)
{
if(array_key_exists($iID, $aCopyData))
{
$aCopyData[$iID] = $aOriginalDatum;
}
}
$rFile = fopen('output.csv', 'w');
foreach($aCopyData as $aCopyDatum)
{
fputcsv_eol($rFile, $aCopyDatum, ',', '"',"rn");
}
fclose($rFile);
I've created a little soulution. If order is important you don't have to index the array and loop through the copied array.
<?php
if(file_exists('output.csv'))
{
unlink('output.csv');
}
function fputcsv_eol($handle, $array, $delimiter = ',', $enclosure = '"', $eol = "n") {
$return = fputcsv($handle, $array, $delimiter, $enclosure);
if($return !== FALSE && "n" != $eol && 0 === fseek($handle, -1, SEEK_CUR)) {
fwrite($handle, $eol);
}
return $return;
}
function scanFile($sFilename, $iIndexColumn)
{
$rFile = fopen($sFilename, 'r');
$aData = array();
while(($aLine = fgetcsv($rFile)) !== false)
{
$aData[$aLine[$iIndexColumn]] = $aLine;
}
fclose($rFile);
return $aData;
}
$iIndexColumn = 0;
$iValueColum = 2;
$aOriginalData = scanFile('original.csv', 0);
$aCopyData = scanFile('copy.csv', 0);
foreach($aOriginalData as $iID => $aOriginalDatum)
{
if(array_key_exists($iID, $aCopyData))
{
$aCopyData[$iID] = $aOriginalDatum;
}
}
$rFile = fopen('output.csv', 'w');
foreach($aCopyData as $aCopyDatum)
{
fputcsv_eol($rFile, $aCopyDatum, ',', '"',"rn");
}
fclose($rFile);
edited Nov 15 '18 at 15:01
answered Nov 14 '18 at 15:30
finder2finder2
61211
61211
fputcsv() expects parameter 2 to be array, boolean given
– user9819807
Nov 14 '18 at 19:44
I'm only mobile at the moment. I think I've fixed it. I'll come back tomorrow to test it probably.
– finder2
Nov 14 '18 at 19:48
ok.................
– user9819807
Nov 15 '18 at 13:19
@doki the example files (changed the separator in the file to ",") are correctly interpreted
– finder2
Nov 15 '18 at 14:23
this code is still not good, it put all values in one row horizontally?
– user9819807
Nov 15 '18 at 14:37
|
show 3 more comments
fputcsv() expects parameter 2 to be array, boolean given
– user9819807
Nov 14 '18 at 19:44
I'm only mobile at the moment. I think I've fixed it. I'll come back tomorrow to test it probably.
– finder2
Nov 14 '18 at 19:48
ok.................
– user9819807
Nov 15 '18 at 13:19
@doki the example files (changed the separator in the file to ",") are correctly interpreted
– finder2
Nov 15 '18 at 14:23
this code is still not good, it put all values in one row horizontally?
– user9819807
Nov 15 '18 at 14:37
fputcsv() expects parameter 2 to be array, boolean given
– user9819807
Nov 14 '18 at 19:44
fputcsv() expects parameter 2 to be array, boolean given
– user9819807
Nov 14 '18 at 19:44
I'm only mobile at the moment. I think I've fixed it. I'll come back tomorrow to test it probably.
– finder2
Nov 14 '18 at 19:48
I'm only mobile at the moment. I think I've fixed it. I'll come back tomorrow to test it probably.
– finder2
Nov 14 '18 at 19:48
ok.................
– user9819807
Nov 15 '18 at 13:19
ok.................
– user9819807
Nov 15 '18 at 13:19
@doki the example files (changed the separator in the file to ",") are correctly interpreted
– finder2
Nov 15 '18 at 14:23
@doki the example files (changed the separator in the file to ",") are correctly interpreted
– finder2
Nov 15 '18 at 14:23
this code is still not good, it put all values in one row horizontally?
– user9819807
Nov 15 '18 at 14:37
this code is still not good, it put all values in one row horizontally?
– user9819807
Nov 15 '18 at 14:37
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302496%2freplace-rows-from-one-csv-file-to-another-csv-where-id-of-is-the-samephp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You need to get CSV data from both the files then you can use a loop to update data.
– Sayed Mohd Ali
Nov 14 '18 at 14:29
do you have some code example?
– user9819807
Nov 14 '18 at 14:31