PHP fgetcsv fails while explode works fine - php

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---------

Related

Sort arr[] in PHP 50 indexes to 7 indexes

Here is an array that I want to sort. This is taken by an HTML table that is already stored in the database. But here it has 50 indexes. That's the thing I want to reduce to seven.
(
[0] => Array
(
[0] =>
[1] => data
[2] => data
[3] => data
[4] => data
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[2] => Array
(
[0] => 1
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[3] => Array
(
[0] => 2
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[4] => Array
(
[0] => 3
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[5] => Array
(
[0] => 4
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[6] => Array
(
[0] => 5
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[7] => Array
(
[0] => 6
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[8] => Array
(
[0] => 7
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[9] => Array
(
[0] => 8
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
[10] => Array
(
[0] => 9
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] =>
[24] =>
[25] =>
[26] =>
[27] =>
[28] =>
[29] =>
[30] =>
[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>
[40] =>
[41] =>
[42] =>
[43] =>
[44] =>
[45] =>
[46] =>
[47] =>
[48] =>
[49] =>
[50] =>
)
)
I tried with for loops, foreach loops to sort this array shows up to 7 indexes. It should be like this.
(
[0] => Array
(
[0] =>
[1] => data
[2] => data
[3] => data
[4] => data
[5] =>
[6] =>
[7] =>
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
[2] => Array
(
[0] => 1
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[3] => Array
(
[0] => 2
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[4] => Array
(
[0] => 3
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[5] => Array
(
[0] => 4
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[6] => Array
(
[0] => 5
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[7] => Array
(
[0] => 6
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[8] => Array
(
[0] => 7
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[9] => Array
(
[0] => 8
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
[10] => Array
(
[0] => 9
[1] => data
[2] => data
[3] => data
[4] => data
[5] => data
[6] =>
[7] =>
)
)
What did I try is,
for ($row = 0; $row <= sizeof($arr); $row++) {
for ($col = 0; $col < 7; $col++) {
$aar[] = $arr[$row][$col];
}
}
This method wasn't worked. What is the way or any suggestions on this?
If you just want to keep x amount of elements from each sub-array, you can use array_slice():
$newArray = [];
foreach($arr as $sub) {
// The example array you say you want have 8 elements, indexes 0-7
// so start with index 0 and take 8
$newArray[] = array_slice($sub, 0, 8);
}
Here's a demo: https://3v4l.org/WhFm4
Here is the one possible solution to your problem. Just define the key limit in if condition. Hope it will solve your problem.
$row = [
[
'',
'data',
'data',
'data',
'',
'',
'',
''
],
[
'',
'data',
'data',
'data',
'',
'',
'',
''
]
];
$reducedArray = [];
foreach ($row as $col) {
$tempArray = [];
foreach ($col as $innnerKey => $innerValue) {
// Define your index limit here.
// As you can see I have set the limit to 7
if ($innnerKey <= 7) {
$tempArray[] = $innerValue;
}
}
$reducedArray[] = $tempArray;
}
echo '<pre>';
print_r($row);
echo '</pre>';
echo '<br/>';
echo '<pre>';
print_r($reducedArray);
echo '</pre>';

php array merge not giving expected/desired results

With the following code I am able to generate 2 arrays..
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
//ignores first line of csv
fgetcsv($file_handle);
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
}
// Set path to CSV file
$csvFile = 'csv1.csv';
$csvFile2 = 'csv2.csv';
$csv = readCSV($csvFile);
$csv2 = readCSV($csvFile2);
echo '<pre>';
$arr = [];
//$csv is your array
foreach($csv as $key => $value){
if(!array_key_exists($value[24],$arr)){
$arr[$value[24]] = [];
}
$arr[$value[24]] = array_merge($arr[$value[24]],$value);
}
//ignores last item in array
array_pop($arr);
$arr2 = [];
//$csv2 is your array
foreach($csv2 as $key => $value){
if(!array_key_exists($value[1],$arr2)){
$arr2[$value[1]] = [];
}
$arr2[$value[1]] = array_merge($arr2[$value[1]],$value);
}
echo '<pre>';
print_r($arr);
print_r($arr2);
echo '</pre>';
This outputs the arrays as follows..
Array 1 ($arr)
Array
(
[1593608448] => Array
(
[0] => 03/25/20
[1] => Vinyl Decal Sticker
[2] =>
[3] => 1
[4] => 2.85
[5] =>
[6] =>
[7] => 0.00
[8] => 0.00
[9] => 2.49
[10] => 0
[11] => 2.85
[12] => GBP
[13] => 1829006957
[14] => 627718667
[15] => 03/25/2020
[16] => 03/25/2020
[17] => John Smith
[18] => Any Street
[19] =>
[20] => Somewhere
[21] => Someplace
[22] => PC0 DE
[23] => United Kingdom
[24] => 1593608448
[25] => Colour:Yellow
[26] => online
[27] => listing
[28] => online_cc
[29] =>
[30] =>
[31] => 0
)
)
And array 2 ($arr2)
Array
(
[1593608448] => Array
(
[0] => 03/25/20
[1] => 1593608448
[2] =>
[3] => John Smith
[4] => John
[5] => Smith
[6] => 1
[7] => Credit Card
[8] => 03/25/20
[9] => Any Street
[10] =>
[11] => Somewhere
[12] => Someplace
[13] => PC 0DE
[14] => United Kingdom
[15] => GBP
[16] => 2.85
[17] =>
[18] =>
[19] => 0.00
[20] => 0.00
[21] => 2.49
[22] => 0
[23] => 5.88
[24] =>
[25] => 0.44
[26] => 4.9
[27] =>
[28] =>
[29] =>
[30] => John Smith
[31] => online
[32] => online_cc
[33] =>
[34] =>
)
)
I have attempted to merge these arrays with..
$merged = array_merge($arr,$arr2);
print_r($merged);
This outputs as..
Array
(
[0] => Array
(
[0] => 03/25/20
[1] => Vinyl Decal Sticker
[2] =>
[3] => 1
[4] => 2.85
[5] =>
[6] =>
[7] => 0.00
[8] => 0.00
[9] => 2.49
[10] => 0
[11] => 2.85
[12] => GBP
[13] => 1829006957
[14] => 627718667
[15] => 03/25/2020
[16] => 03/25/2020
[17] => John Smith
[18] => Any Street
[19] =>
[20] => Somewhere
[21] => Someplace
[22] => PC0 DE
[23] => United Kingdom
[24] => 1593608448
[25] => Colour:Yellow
[26] => online
[27] => listing
[28] => online_cc
[29] =>
[30] =>
[31] => 0
)
[1] => Array
(
[0] => 03/25/20
[1] => 1593608448
[2] =>
[3] => John Smith
[4] => John
[5] => Smith
[6] => 1
[7] => Credit Card
[8] => 03/25/20
[9] => Any Street
[10] =>
[11] => Somewhere
[12] => Someplace
[13] => PC 0DE
[14] => United Kingdom
[15] => GBP
[16] => 2.85
[17] =>
[18] =>
[19] => 0.00
[20] => 0.00
[21] => 2.49
[22] => 0
[23] => 5.88
[24] =>
[25] => 0.44
[26] => 4.9
[27] =>
[28] =>
[29] =>
[30] => John Smith
[31] => online
[32] => online_cc
[33] =>
[34] =>
)
)
When I wanted/expected..
Array
(
[1593608448] => Array
(
[0] => 03/25/20
[1] => Vinyl Decal Sticker
[2] =>
[3] => 1
[4] => 2.85
[5] =>
[6] =>
[7] => 0.00
[8] => 0.00
[9] => 2.49
[10] => 0
[11] => 2.85
[12] => GBP
[13] => 1829006957
[14] => 627718667
[15] => 03/25/2020
[16] => 03/25/2020
[17] => John Smith
[18] => Any Street
[19] =>
[20] => Somewhere
[21] => Someplace
[22] => PC0 DE
[23] => United Kingdom
[24] => 1593608448
[25] => Colour:Yellow
[26] => online
[27] => listing
[28] => online_cc
[29] =>
[30] =>
[31] => 0
[32] => 03/25/20
[33] => 1593608448
[34] =>
[35] => John Smith
[36] => John
[37] => Smith
[38] => 1
[39] => Credit Card
[40] => 03/25/20
[41] => Any Street
[42] =>
[43] => Somewhere
[44] => Someplace
[45] => PC 0DE
[46] => United Kingdom
[47] => GBP
[48] => 2.85
[49] =>
[50] =>
[51] => 0.00
[52] => 0.00
[53] => 2.49
[54] => 0
[55] => 5.88
[56] =>
[57] => 0.44
[58] => 4.9
[59] =>
[60] =>
[61] =>
[62] => John Smith
[63] => online
[64] => online_cc
[65] =>
[66] =>
)
)
So keeping the order id (which is always [24] in 1 arr1 and [2] in the arr2) as the key then add on to the end of the matching key in $arr1 the contents of the matching key from $arr2 with the numbers just following on.
I have looked at many similar questions and answers on here but not getting the results I require.
Or another way of putting it is it will check if [24] and [2] match and then append on to the end
It would be better to merge the data as you read it. I was going to change your read so that it read the CSV as it created the arrays, but I have left the readCSV() as it is - except to add array_filter() to remove empty elements.
Rather than create the two arrays and then merge them, I've change the second CSV loop to check $arr and add the data at this point...
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
//ignores first line of csv
fgetcsv($file_handle);
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
$line_of_text = array_filter($line_of_text);
return $line_of_text;
}
$csvFile = 'csv1.csv';
$csvFile2 = 'csv2.csv';
$csv = readCSV($csvFile);
$csv2 = readCSV($csvFile2);
echo '<pre>';
$arr = [];
//$csv is your array
foreach($csv as $key => $value){
if(!array_key_exists($value[24],$arr)){
$arr[$value[24]] = [];
}
$arr[$value[24]] = array_merge($arr[$value[24]],$value);
}
foreach($csv2 as $key => $value){
if(!array_key_exists($value[1],$arr)){
$arr[$value[1]] = [];
}
$arr[$value[1]] = array_merge($arr[$value[1]],$value);
}
echo '<pre>';
print_r($arr);
echo '</pre>';
Use array_merge_recursive instead of array_merge
$merged = array_merge_recursive($arr,$arr2);

PHP multi dimensional array search by conditions

I am developing a financial web application I am really stuck with this issue.
I need to search with conditions and return the array if the condition is true..
I know the multi-dimensional array search, but I did't get any idea for this. For example :
if array[0][7] > array[0][8] && array[0][12] < array[0][15]
Please suggest me the solution
Array (
[0] => Array (
[0] => 54452
[1] => 'KSB'
[2] => 'INE999A01015'
[3] => 'EQ'
[4] => 'Ksb Limited'
[5] => -0.70
[6] => -0.10
[7] => 662.90
[8] => 663.60
[9] => 669.35
[10] => 678.25
[11] => 651.55
[12] => 7874
[13] => 676.91
[14] => 690.93
[15] => 703.61
[16] => 664.23
[17] => 650.21
[18] => 637.53
[19] => 623.51
[20] => 2530
[21] => 32.13
[22] => 825.00
[23] => 539.00
[24] => 665.89
[25] => 631.37
[26] => 616.65
[27] => 610.14
[28] => 615.89
[29] => 656.25
[30] => 680.74
[31] => 60.71
[32] => 60.71
[33] => 13.62
[34] => 29.39
[35] => -38.97
[36] => 129.47
[37] => 2019-09-26
[38] => 27632
),
[1] => Array (
[0] => 53772
[1] => 'ASPINWALL'
[2] => 'INE991I01015'
[3] => 'EQ'
[4] => 'Aspinwall And Company Limited'
[5] => -10.50
[6] => -7.74
[7] => 127.45
[8] => 137.95
[9] => 135.60
[10] => 144.50
[11] => 112.60
[12] => 9583
[13] => 143.76
[14] => 160.08
[15] => 175.66
[16] => 128.18
[17] => 111.86
[18] => 96.28
[19] => 79.96
[20] => 5108
[21] => 53.30
[22] => 214.80
[23] => 112.60
[24] => 135.17
[25] => 137.13
[26] => 137.46
[27] => 136.86
[28] => 137.59
[29] => 143.00
[30] => 153.89
[31] => 39.41
[32] => 39.41
[33] => -0.90
[34] => 37.96
[35] => -58.05
[36] => -361.91
[37] => 2019-09-26
[38] => 26324
)
)
Thanks in advance
Give a try to following code and make necessary changes according to your need
function search_sg_dma($array, $pre_close, $dma_value, $close_price){
...
$result = array();
for( $i = 0; $i < count( $array ); $i++ ) {
if ( $array[$i][$pre_close] < $array[$i][$dma_value] && $array[$i][$close_price] > $array[$i][$dma_value] ) {
$result[] = array[$i];
}
}
return $result;
}

Cut array in php

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 );

Regex match every character and newline

I want to filter some data using regex. As for now I have some text going over 2 lines and I tried to make the linebreak match with [^.*]. But it seems to not pass the newline and so it doesn't match any result on the second line. How can I include the linebreak? I tried something like [^\n\r.*] but it didn't worked out.
Description
You could use the 's' option which forces the dot to match all new line characters, or if you don't have control over the underlying code you could try:
([^.]|[.])
This will match every character. The dot will sometimes not match the carriage return, new line.
PHP example
<?php
$sourcestring="This is my.
super cool
test string";
preg_match_all('/([^.]|[.])/i',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
$matches Array:
(
[0] => Array
(
[0] => T
[1] => h
[2] => i
[3] => s
[4] =>
[5] => i
[6] => s
[7] =>
[8] => m
[9] => y
[10] => .
[11] =>
[12] =>
[13] => s
[14] => u
[15] => p
[16] => e
[17] => r
[18] =>
[19] => c
[20] => o
[21] => o
[22] => l
[23] =>
[24] =>
[25] => t
[26] => e
[27] => s
[28] => t
[29] =>
[30] => s
[31] => t
[32] => r
[33] => i
[34] => n
[35] => g
)
[1] => Array
(
[0] => T
[1] => h
[2] => i
[3] => s
[4] =>
[5] => i
[6] => s
[7] =>
[8] => m
[9] => y
[10] => .
[11] =>
[12] =>
[13] => s
[14] => u
[15] => p
[16] => e
[17] => r
[18] =>
[19] => c
[20] => o
[21] => o
[22] => l
[23] =>
[24] =>
[25] => t
[26] => e
[27] => s
[28] => t
[29] =>
[30] => s
[31] => t
[32] => r
[33] => i
[34] => n
[35] => g
)
)

Categories