Parsing a CSV file for getting an array some lines are not parsed correctly with fgetcsv; tried with "100", "0" and "1000" line length while the csv file lines were smaller than 80 chars; The separator char was ";":
fgetcsv($h, 0, ";");
I did the same with explode and everything works fine:
$line = fgets($handle);
$pieces = explode(";", $line);
I will use the latest, but, still, I would like to understand what was wrong with fgetcsv. Any idea?
Here more details:
CODE
$h = fopen(FCPATH . $file_dir, "r");
$handle = fopen(FCPATH . $file_dir, "r");
$header = fgetcsv($h, 0, ";");
$line = fgets($handle);
while (($data = fgetcsv($h, 1000, ";")) !== FALSE) {
$line = fgets($handle);
log_message('error', "\n-----------START-------\n\t". print_r($line,1));
log_message('error', "\n\t". print_r($data,1));
$pieces = explode(";", $line);
log_message('error', "\n\t". print_r($pieces,1)."\n-----------END---------\n\n");
//... other code
}
LOG OUTPUT
many lines parsed correctly like this
RIGHE DATI
ERROR - 2019-02-16 19:21:47 -->
-----------START-------
2538;;;Emergency Contraception;http://track.healthtrader.com/track.php?c=cmlkPTcxMjMyOSZhaWQ9NTQ1NDI0MjU;;;;ellaOne;http://track.healthtrader.com/track.php?c=cmlkPTcxMjMzMCZhaWQ9NTQ1NDI0MjU;pills;;;;;http://www.euroclinix.net/en/images/product/ellaone-s.jpg;;http://www.euroclinix.net/en/images/product/ellaone-m.jpg;Emergency Contraceptive: ellaOne Pill;http://www.euroclinix.net/en/images/product/ellaone-l.jpg;;;Y;0;GBP;œ;8995;;http://track.healthtrader.com/track.php?c=cmlkPTcxNjExNiZhaWQ9NTQ1NDI0MjU;Emergency Contraception<br>;;ellaOne 30mg x 2;http://www.euroclinix.net/en/images/product/hra-pharma-logo.jpg;HRA Pharma;http://www.euroclinix.net/en/images/product/ellaone-t.jpg;
ERROR - 2019-02-16 19:21:47 -->
Array
(
[0] => 2538
[1] =>
[2] =>
[3] => Emergency Contraception
[4] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjMyOSZhaWQ9NTQ1NDI0MjU
[5] =>
[6] =>
[7] =>
[8] => ellaOne
[9] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjMzMCZhaWQ9NTQ1NDI0MjU
[10] => pills
[11] =>
[12] =>
[13] =>
[14] =>
[15] => http://www.euroclinix.net/en/images/product/ellaone-s.jpg
[16] =>
[17] => http://www.euroclinix.net/en/images/product/ellaone-m.jpg
[18] => Emergency Contraceptive: ellaOne Pill
[19] => http://www.euroclinix.net/en/images/product/ellaone-l.jpg
[20] =>
[21] =>
[22] => Y
[23] => 0
[24] => GBP
[25] => œ
[26] => 8995
[27] =>
[28] => http://track.healthtrader.com/track.php?c=cmlkPTcxNjExNiZhaWQ9NTQ1NDI0MjU
[29] => Emergency Contraception<br>
[30] =>
[31] => ellaOne 30mg x 2
[32] => http://www.euroclinix.net/en/images/product/hra-pharma-logo.jpg
[33] => HRA Pharma
[34] => http://www.euroclinix.net/en/images/product/ellaone-t.jpg
[35] =>
)
ERROR - 2019-02-16 19:21:47 -->
Array
(
[0] => 2538
[1] =>
[2] =>
[3] => Emergency Contraception
[4] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjMyOSZhaWQ9NTQ1NDI0MjU
[5] =>
[6] =>
[7] =>
[8] => ellaOne
[9] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjMzMCZhaWQ9NTQ1NDI0MjU
[10] => pills
[11] =>
[12] =>
[13] =>
[14] =>
[15] => http://www.euroclinix.net/en/images/product/ellaone-s.jpg
[16] =>
[17] => http://www.euroclinix.net/en/images/product/ellaone-m.jpg
[18] => Emergency Contraceptive: ellaOne Pill
[19] => http://www.euroclinix.net/en/images/product/ellaone-l.jpg
[20] =>
[21] =>
[22] => Y
[23] => 0
[24] => GBP
[25] => œ
[26] => 8995
[27] =>
[28] => http://track.healthtrader.com/track.php?c=cmlkPTcxNjExNiZhaWQ9NTQ1NDI0MjU
[29] => Emergency Contraception<br>
[30] =>
[31] => ellaOne 30mg x 2
[32] => http://www.euroclinix.net/en/images/product/hra-pharma-logo.jpg
[33] => HRA Pharma
[34] => http://www.euroclinix.net/en/images/product/ellaone-t.jpg
[35] =>
)
-----------END---------
Some lines not parsed correctly by fgetcsv, but still parsed correctly by explode, as in this case
ERROR - 2019-02-16 19:21:47 -->
-----------START-------
7667;;;Diabetes;http://track.healthtrader.com/track.php?c=cmlkPTcxMjI1MCZhaWQ9NTQ1NDI0MjU;;;;Metformin;http://track.healthtrader.com/track.php?c=cmlkPTcxMjI1MyZhaWQ9NTQ1NDI0MjU;pills;Teva;;;;http://www.euroclinix.net/en/images/product/metformin-pack-s.jpg;;http://www.euroclinix.net/en/images/product/metformin-pack-m.jpg;;http://www.euroclinix.net/en/images/product/metformin-l.jpg;Large view of sealed box pack that contains Metformin 500mg tablets. ;;Y;0;GBP;œ;2995;;http://track.healthtrader.com/track.php?c=cmlkPTcxNjQ1MCZhaWQ9NTQ1NDI0MjU;Diabetes<br>;;Metformin 500mg x 168;http://www.euroclinix.net/en/images/product/pfizer_logo.gif;Pfizer Pharmaceutical;http://www.euroclinix.net/en/images/product/metformin-pack-t.jpg;
ERROR - 2019-02-16 19:21:47 -->
Array
(
[0] => 1686
[1] =>
[2] =>
[3] => Erectile Dysfunction
[4] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjI1NCZhaWQ9NTQ1NDI0MjU
[5] =>
[6] =>
[7] =>
[8] => Cialis
[9] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjI1NSZhaWQ9NTQ1NDI0MjU
[10] => pills
[11] => Lilly
[12] =>
[13] => Cialis (tadalafil) is a fast acting prescription treatment for men suffering with erectile problems
[14] => Cialis (tadalafil) is a prescription impotence treatment that helps men with erectile problems. Cialis helps men to get an erection and sustain an erection, working in a little as 30 minutes. Cialis is the longest lasting impotence medication, with benefits experienced for up to 36 hours.
[15] => http://www.euroclinix.net/en/images/product/cialis-s.jpg
[16] =>
[17] => http://www.euroclinix.net/en/images/product/cialis-m.jpg
[18] =>
[19] => http://www.euroclinix.net/en/images/product/cialis-l.jpg
[20] => Cialis medication is sealed inside a box for better protection.
[21] =>
[22] => Y
[23] => 0
[24] => GBP
[25] => œ
[26] => 18495
[27] =>
[28] => http://track.healthtrader.com/track.php?c=cmlkPTcxNjM3NiZhaWQ9NTQ1NDI0MjU
[29] => Impotence<br>
[30] =>
[31] => Cialis 10mg x 12
[32] => http://www.euroclinix.net/en/images/product/lilly-
)
ERROR - 2019-02-16 19:21:47 -->
Array
(
[0] => 7667
[1] =>
[2] =>
[3] => Diabetes
[4] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjI1MCZhaWQ9NTQ1NDI0MjU
[5] =>
[6] =>
[7] =>
[8] => Metformin
[9] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjI1MyZhaWQ9NTQ1NDI0MjU
[10] => pills
[11] => Teva
[12] =>
[13] =>
[14] =>
[15] => http://www.euroclinix.net/en/images/product/metformin-pack-s.jpg
[16] =>
[17] => http://www.euroclinix.net/en/images/product/metformin-pack-m.jpg
[18] =>
[19] => http://www.euroclinix.net/en/images/product/metformin-l.jpg
[20] => Large view of sealed box pack that contains Metformin 500mg tablets.
[21] =>
[22] => Y
[23] => 0
[24] => GBP
[25] => œ
[26] => 2995
[27] =>
[28] => http://track.healthtrader.com/track.php?c=cmlkPTcxNjQ1MCZhaWQ9NTQ1NDI0MjU
[29] => Diabetes<br>
[30] =>
[31] => Metformin 500mg x 168
[32] => http://www.euroclinix.net/en/images/product/pfizer_logo.gif
[33] => Pfizer Pharmaceutical
[34] => http://www.euroclinix.net/en/images/product/metformin-pack-t.jpg
[35] =>
)
-----------END---------
I am trying to get my image slides to display in order by number with the following:
$image=array();
$img_folder = $params->get('path');
mt_srand((double)microtime()*1000);
if(is_dir($img_folder)){
krsort($file);
$imgs = dir($img_folder);
while ($file = $imgs->read()) {
if ((eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file)))
$image[] = "$file";
} closedir($imgs->handle);
print_r($image);
return $image;
}
However, the array works but the order is off:
Array ( [0] => Slide1.png [1] => Slide10.png [2] => Slide11.png [3] => Slide12.png [4] => Slide13.png [5] => Slide14.png [6] => Slide15.png [7] => Slide16.png [8] => Slide17.png [9] => Slide18.png [10] => Slide19.png [11] => Slide2.png [12] => Slide20.png [13] => Slide21.png [14] => Slide22.png [15] => Slide23.png [16] => Slide24.png [17] => Slide25.png [18] => Slide26.png [19] => Slide27.png [20] => Slide28.png [21] => Slide29.png [22] => Slide3.png [23] => Slide30.png [24] => Slide31.png [25] => Slide4.png [26] => Slide5.png [27] => Slide6.png [28] => Slide7.png [29] => Slide8.png [30] => Slide9.png )
If I put natsort($image); after $image[] = "$file"; the order is correct but the array is off:
Array ( [0] => Slide1.png [11] => Slide2.png [22] => Slide3.png [25] => Slide4.png [26] => Slide5.png [27] => Slide6.png [28] => Slide7.png [29] => Slide8.png [30] => Slide9.png [1] => Slide10.png [2] => Slide11.png [3] => Slide12.png [4] => Slide13.png [5] => Slide14.png [6] => Slide15.png [7] => Slide16.png [8] => Slide17.png [9] => Slide18.png [10] => Slide19.png [12] => Slide20.png [13] => Slide21.png [14] => Slide22.png [15] => Slide23.png [16] => Slide24.png [17] => Slide25.png [18] => Slide26.png [19] => Slide27.png [20] => Slide28.png [21] => Slide29.png [23] => Slide30.png [24] => Slide31.png )
I need them to order Slide1 - Slide30, not like listed above. I could change the names from numeric (Slide1) to alphabetic (SlideA) but would like to keep this naming convention. Is there a way to do this without changing the image names?
This image slide is a "how to" slide show.
NatSort is what you are looking for.
just use natsort($image);
You can reference it at:
http://php.net/manual/es/function.natsort.php
I didn't know about natsort(), this should do it too though..
$imgs = scandir(dir($img_folder));
$sorted = array();
foreach($imgs as $img){
$index = intval(trim($img, "Slide.png"));
$sorted[$index] = $img;
}
ksort($sorted);
$sorted = array_values($sorted);
You can use natsort() function
natsort
e.g.
$array1 = array("img12.png", "img10.png", "img2.png", "img1.png");
natsort($array);
print_r($array);
This way you will get out put is :
Array ( [3] => img1.png [2] => img2.png [1] => img10.png [0] => img12.png )
You can also use natcasesort($image). It sort an array using a case insensitive "natural order" algorithm.
This did the trick!!!
Just after
$image[] = "$file";
I added
sort($image, SORT_NATURAL | SORT_FLAG_CASE);
This correctly orders the images in the array and the order:
Array ( [0] => Slide1.png [1] => Slide2.png [2] => Slide3.png [3] => Slide4.png [4] => Slide5.png [5] => Slide6.png [6] => Slide7.png [7] => Slide8.png [8] => Slide9.png [9] => Slide10.png [10] => Slide11.png [11] => Slide12.png [12] => Slide13.png [13] => Slide14.png [14] => Slide15.png [15] => Slide16.png [16] => Slide17.png [17] => Slide18.png [18] => Slide19.png [19] => Slide20.png [20] => Slide21.png [21] => Slide22.png [22] => Slide23.png [23] => Slide24.png [24] => Slide25.png [25] => Slide26.png [26] => Slide27.png [27] => Slide28.png [28] => Slide29.png [29] => Slide30.png [30] => Slide31.png )
I want to cut array in php. My array is listed below :
Array
(
[0] => 6/1/2014
[1] => 6/2/2014
[2] => 6/3/2014
[3] => 6/4/2014
[4] => 6/5/2014
[5] => 6/6/2014
[6] => 6/7/2014
[7] => 6/8/2014
[8] => 6/9/2014
[9] => 6/10/2014
[10] => 6/11/2014
[11] => 6/12/2014
[12] => 6/13/2014
[13] => 6/14/2014
[14] => 6/15/2014
[15] => 6/16/2014
[16] => 6/17/2014
[17] => 6/18/2014
[18] => 6/19/2014
[19] => 6/20/2014
[20] => 6/21/2014
[21] => 6/22/2014
[22] => 6/23/2014
[23] => 6/24/2014
[24] => 6/25/2014
[25] => 6/26/2014
[26] => 6/27/2014
[27] => 6/28/2014
[28] => 6/29/2014
[29] => 6/30/2014
[30] => 7/1/2014
[31] => 7/2/2014
[32] => 7/3/2014
[33] => 7/4/2014
[34] => 7/5/2014
[35] => 7/6/2014
[36] => 7/7/2014
[37] => 7/8/2014
[38] => 7/9/2014
[39] => 7/10/2014
[40] => 7/11/2014
[41] => 7/12/2014
[42] => 7/13/2014
[43] => 7/14/2014
[44] => 7/15/2014
[45] => 7/16/2014
[46] => 7/17/2014
[47] => 7/18/2014
[48] => 7/19/2014
[49] => 7/20/2014
[50] => 7/21/2014
[51] => 7/22/2014
[52] => 7/23/2014
[53] => 7/24/2014
[54] => 7/25/2014
[55] => 7/26/2014
[56] => 7/27/2014
[57] => 7/28/2014
[58] => 7/29/2014
[59] => 7/30/2014
[60] => 7/31/2014
[61] => 8/1/2014
)
In this array 0 to 29 elements if for 6th Month, 30th to 60th elements are for 7th Month etc..
Now i want this array in the below fashion
Array
(
[0] => 6/1/2014
[1] => 6/2/2014
[2] => 6/3/2014
[3] => 6/4/2014
[4] => 6/5/2014
[5] => 6/6/2014
[6] => 6/7/2014
[7] => 6/8/2014
[8] => 6/9/2014
[9] => 6/10/2014
[10] => 6/11/2014
[11] => 6/12/2014
[12] => 6/13/2014
[13] => 6/14/2014
[14] => 6/15/2014
[15] => 6/16/2014
[16] => 6/17/2014
[17] => 6/18/2014
[18] => 6/19/2014
[19] => 6/20/2014
[20] => 6/21/2014
[21] => 6/22/2014
[22] => 6/23/2014
[23] => 6/24/2014
[24] => 6/25/2014
[25] => 6/26/2014
[26] => 6/27/2014
[27] => 6/28/2014
[28] => 6/29/2014
[29] => 6/30/2014
)
Array
(
[0] => 7/1/2014
[1] => 7/2/2014
[2] => 7/3/2014
[3] => 7/4/2014
[4] => 7/5/2014
[5] => 7/6/2014
[6] => 7/7/2014
[7] => 7/8/2014
[8] => 7/9/2014
[9] => 7/10/2014
[10] => 7/11/2014
[11] => 7/12/2014
[12] => 7/13/2014
[13] => 7/14/2014
[14] => 7/15/2014
[15] => 7/16/2014
[16] => 7/17/2014
[17] => 7/18/2014
[18] => 7/19/2014
[19] => 7/20/2014
[20] => 7/21/2014
[21] => 7/22/2014
[22] => 7/23/2014
[23] => 7/24/2014
[24] => 7/25/2014
[25] => 7/26/2014
[26] => 7/27/2014
[27] => 7/28/2014
[28] => 7/29/2014
[29] => 7/30/2014
[30] => 7/31/2014
)
Array
(
[0] => 8/1/2014
)
This calculation should be in a way that if i chose other months then it also do the same process of cutting array for different months.
you can also separate array by month & and put them in main array
$arrres =array();
foreach($arr as $value)
{
$arrres[str_replace('/','',substr($value,0,2))][] = $value ;
}
print_r($arrres);
you can use this:, it will also generate arrays with same num of days in months (28,30,31)
//generate dates for test
$dates = [];
for($i = 0 ; $i < 90 ; $i++){
$dates[] = Date("d/m/Y",time()-($i*24*3600));
}
//make results, also validates that it's same month and year
$result = [];
foreach($dates as $date){
$dStr = Date("m-y",strtotime(str_replace('/', '-', $date)));
if(!isset($result[$dStr])) $result[$dStr] = [];
$result[$dStr][] = $date;
}
var_dump($result);
I'd explode the value of every index in your array and set it as an index to the new array.
$days = [];
foreach($your_array as $day) {
$days[ explode("/", $day)[0] ][] = $day;
}
var_dump( $days );
I have a multi dim. array with duplicate values. I need it to show only one single value of each in a dropdown, which in turn are used for a search Query. The indexs are not important here.
I tried with array_map, serialize, array_unique and so on, but can't get it right. Some values allways comes out twice.
I'm on php version 4.3.2, so can't use functions made after that version.
The array looks like this:
Array
(
[1] => Array
(
[1] => 0,9
[2] => 1,3
[3] => 1,6
[4] => 1,5
[5] => 0,0
[6] => 1,2
)
[2] => Array
(
[3] => 1,0
[4] => 2,0
[5] => 1,78
[6] => 1,44
[7] => 1,87
[8] => 1,56
[9] => 2,4
[10] => 2,93
[11] => 2,50
[12] => 1,4
[13] => 6,0
[14] => 4,0
[15] => 1,80
[16] => 3,00
)
[3] => Array
(
[5] => 1,0
[6] => 1,2
[7] => 1,5
[8] => 1,6
)
[4] => Array
(
[7] => 1,0
)
[5] => Array
(
[1] => 1,0
[2] => 1,5
[3] => 2,0
[4] => 2,5
[5] => 3,0
[6] => 3,5
[7] => 4,0
[8] => 4,5
)
[6] => Array
(
[1] => 1,5
[2] => 2,0
[3] => 2,5
[4] => 3,0
[5] => 3,5
[6] => 4,0
[7] => 4,5
[8] => 5,0
[9] => 5,5
[10] => 6,0
[11] => 6,5
[12] => 7,0
[13] => 7,5
[14] => 8,0
[15] => 8,5
[16] => 9,0
[17] => 9,5
[18] => 10,0
)
[7] => Array
(
[15] => 0,65
[16] => 0,75
[17] => 0,85
[18] => 0,90
[19] => 1,00
[20] => 1,10
[21] => 1,15
[22] => 1,25
[23] => 1,50
[24] => 1,55
[25] => 1,60
[26] => 1,80
[27] => 1,85
[28] => 2,00
[29] => 2,10
)
[8] => Array
(
[30] => 0,90
[31] => 1,00
)
[10] => Array
(
[34] => 0,65
[35] => 0,75
[36] => 0,85
[37] => 0,90
[38] => 1,00
[39] => 1,10
[40] => 1,15
[41] => 1,25
[42] => 1,50
[43] => 1,55
[44] => 1,60
[45] => 1,80
[46] => 1,85
[47] => 2,00
[48] => 2,10
)
[11] => Array
(
[34] => 1,0
[35] => 1,5
[36] => 2,0
[37] => 2,5
[38] => 3,0
[39] => 3,5
[40] => 4,0
[41] => 4,5
)
[12] => Array
(
[43] => 1,00
)
[13] => Array
(
[45] => 2,00
[46] => 2,20
[47] => 2,50
[48] => 2,75
[49] => 3,00
[50] => 3,25
[51] => 3,50
[52] => 2,60
[53] => 2,85
[54] => 1,50
[55] => 2,10
[56] => 2,35
[57] => 2,80
[58] => 2,40
[59] => 2,70
[60] => 2,25
[61] => 3,40
[62] => 2,55
[63] => 2,30
[64] => 2,65
)
)
Any help would be appreciated.
To make each sub-array unique compared to other sub-arrays (the order is important):
$array = array_map('unserialize', array_unique(array_map('serialize', $array)));
To make only values within each sub-array unique:
$array = array_map('array_unique', $array);
$array = array_map('unserialize', array_unique(array_map('serialize', $array)));
is exactly what I tried, and from that same thread, but it comes out like this:
Array(
[1] => Array
(
[1] => 0,9
[2] => 1,3
[3] => 1,6
[4] => 1,5
[5] => 0,0
[6] => 1,2
)
[2] => Array
(
[3] => 1,0
[4] => 2,0
[5] => 1,78
[6] => 1,44
[7] => 1,87
[8] => 1,56
[9] => 2,4
[10] => 2,93
[11] => 2,50
[12] => 1,4
[13] => 6,0
[14] => 4,0
[15] => 1,80
[16] => 3,00
)
[3] => Array
(
[5] => 1,0
[6] => 1,2
[7] => 1,5
[8] => 1,6
)
[4] => Array
(
[7] => 1,0
)
[5] => Array
(
[1] => 1,0
[2] => 1,5
[3] => 2,0
[4] => 2,5
[5] => 3,0
[6] => 3,5
[7] => 4,0
[8] => 4,5
)
[6] => Array
(
[1] => 1,5
[2] => 2,0
[3] => 2,5
[4] => 3,0
[5] => 3,5
[6] => 4,0
[7] => 4,5
[8] => 5,0
[9] => 5,5
[10] => 6,0
[11] => 6,5
[12] => 7,0
[13] => 7,5
[14] => 8,0
[15] => 8,5
[16] => 9,0
[17] => 9,5
[18] => 10,0
)
[7] => Array
(
[15] => 0,65
[16] => 0,75
[17] => 0,85
[18] => 0,90
[19] => 1,00
[20] => 1,10
[21] => 1,15
[22] => 1,25
[23] => 1,50
[24] => 1,55
[25] => 1,60
[26] => 1,80
[27] => 1,85
[28] => 2,00
[29] => 2,10
)
[8] => Array
(
[30] => 0,90
[31] => 1,00
)
[10] => Array
(
[34] => 0,65
[35] => 0,75
[36] => 0,85
[37] => 0,90
[38] => 1,00
[39] => 1,10
[40] => 1,15
[41] => 1,25
[42] => 1,50
[43] => 1,55
[44] => 1,60
[45] => 1,80
[46] => 1,85
[47] => 2,00
[48] => 2,10
)
[11] => Array
(
[34] => 1,0
[35] => 1,5
[36] => 2,0
[37] => 2,5
[38] => 3,0
[39] => 3,5
[40] => 4,0
[41] => 4,5
)
[12] => Array
(
[43] => 1,00
)
[13] => Array
(
[45] => 2,00
[46] => 2,20
[47] => 2,50
[48] => 2,75
[49] => 3,00
[50] => 3,25
[51] => 3,50
[52] => 2,60
[53] => 2,85
[54] => 1,50
[55] => 2,10
[56] => 2,35
[57] => 2,80
[58] => 2,40
[59] => 2,70
[60] => 2,25
[61] => 3,40
[62] => 2,55
[63] => 2,30
[64] => 2,65
)
)
And I still got multiple values like 1,0