magento
asked on
php simple script
Hi,
Have a csv file, need the output as below:
Input:
ID Product Details
2500 PROD894,PROD888,PROD900,PR OD23,PROD1 7
2501 PROD390
2502 PROD388
2503 PROD440,PROD433,PROD475
Output:
ID Product Details
2500 PROD894
2500 PROD888
2500 PROD900
2500 PROD23
2500 PROD17
2501 PROD390
2502 PROD388
2503 PROD440
2503 PROD443
2503 PROD475
Thanks
Have a csv file, need the output as below:
Input:
ID Product Details
2500 PROD894,PROD888,PROD900,PR
2501 PROD390
2502 PROD388
2503 PROD440,PROD433,PROD475
Output:
ID Product Details
2500 PROD894
2500 PROD888
2500 PROD900
2500 PROD23
2500 PROD17
2501 PROD390
2502 PROD388
2503 PROD440
2503 PROD443
2503 PROD475
Thanks
ASKER
Amar,
I tried the above but for some reason i didnt get the output.
This is the input file i have tried, can you please run and send me the output?
Thanks
test.csv
I tried the above but for some reason i didnt get the output.
This is the input file i have tried, can you please run and send me the output?
Thanks
test.csv
Dear magento,
you can try following code and I tested it works , extraction is starting at after line of "ID Product Details" , and I suppose the character between "2500" and "PRODXXX" is space, otherwise you need to find out the exact delimiter for explode() function in php if it is not a space between them
Duncan
you can try following code and I tested it works , extraction is starting at after line of "ID Product Details" , and I suppose the character between "2500" and "PRODXXX" is space, otherwise you need to find out the exact delimiter for explode() function in php if it is not a space between them
Duncan
<?php
$proverbs=file("input.csv");$cnt=count($proverbs);$kline=array();
for ($i=1;$i<$cnt;$i++){$mline=explode(",",$proverbs[$i]);$cnt_2=count($mline);
for($a=0;$a<$cnt_2;$a++){array_push($kline,trim($mline[$a],"\n")."\n");}}
$cnt=count($kline);
for ($a=0;$a<$cnt-1;$a++){
$mline=explode(" ",$kline[$a]);$next=explode(" ",$kline[$a+1]);
//echo $next[0]."=$a=".$mline[0]."===\n";
if ($next[0]>0){} else {$kline[$a+1]=$mline[0]." ".$kline[$a+1];}
}
file_put_contents("output.csv",$kline);
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Duncab7,
Its a csv file so the delimiter is comma.
I have attached the csv file to previous post .
Not sure, my xampp was not running properly and hence i cant test it. Ca you pls run it and send me the output file. That would be great !!!
Thanks
Its a csv file so the delimiter is comma.
I have attached the csv file to previous post .
Not sure, my xampp was not running properly and hence i cant test it. Ca you pls run it and send me the output file. That would be great !!!
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Duncan ,
Sorry , since i have issue with xampp i cant test the code.
Can you please run the code for the attached file and send me the output?
test.csv
Sorry , since i have issue with xampp i cant test the code.
Can you please run the code for the attached file and send me the output?
test.csv
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hello magento,
did u try my code?
It should work fine for u. you will see expected output in browser window.
Also you should put your test.csv file in same directory as your php file.
How are you running the code? in browser or from command line?
are you getting any error while running the code?
Thank you.
Amar Bardoliwala
did u try my code?
It should work fine for u. you will see expected output in browser window.
Also you should put your test.csv file in same directory as your php file.
How are you running the code? in browser or from command line?
are you getting any error while running the code?
Thank you.
Amar Bardoliwala
Sorry forgot to paste the o/p.
$datadump[$idval][]= trim(trim($dataval),"\n");
line 26 is not required. You can run without that too.
outputs.csv
$datadump[$idval][]= trim(trim($dataval),"\n");
line 26 is not required. You can run without that too.
outputs.csv
Add back the first line of "ID PRODUCT Detail"
You input file format is different on this thread compared to the test.csv, that is
no "PROD", but it will be easier and simple
Please read the attached file
input.csv format is changed from input.csv to test.csv as you request
junk.php is changed to junk2.php
Be reminded your product detail data with " quote at start and end of second column string such as "0,0,0,0"
Duncan
junk2.php
output.csv
You input file format is different on this thread compared to the test.csv, that is
no "PROD", but it will be easier and simple
Please read the attached file
input.csv format is changed from input.csv to test.csv as you request
junk.php is changed to junk2.php
Be reminded your product detail data with " quote at start and end of second column string such as "0,0,0,0"
Duncan
<?php
$proverbs=file("test.csv");
$cnt=count($proverbs);
$kline=array();
array_push($kline,$proverbs[0]);
for ($i=1;$i<$cnt;$i++){$mline=explode(",",$proverbs[$i]);$cnt_2=count($mline);
for($a=1;$a<$cnt_2;$a++){array_push($kline,$mline[0].",".str_replace('"',"",trim($mline[$a],"\n"))."\n");}}
$cnt=count($kline);
file_put_contents("output.csv",$kline);
?>
test.csvjunk2.php
output.csv
@magento please revert whether given code is working as expected or not
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi All,
First i apologies to everybody, still i am facing issue with my Xampp.
I dont know why the apache is not starting and only mysql is running in xampp admin.
Once rectified, i will check every one of the code and provide update.
Thanks,
First i apologies to everybody, still i am facing issue with my Xampp.
I dont know why the apache is not starting and only mysql is running in xampp admin.
Once rectified, i will check every one of the code and provide update.
Thanks,
ASKER
Thanks for all ur kind help and apologies for the delay.
considering that your input file is as above INPUT format and its name is text.csv.
you can try something like following
Open in new window
Hope this will help you.
Thank you.
Amar Bardoliwala