I have an array that represents UNIX time in int type from a table I can't change. Some of the rows are not full UNIX timestamps but are short by a couple of integers. There is a reason this is so in the table but for my script, I need the string to change the non 10 digit rows into "0" and the 10 digit ones into date("Ymd",?) form. Here's an example of the array $qucls:
Array
(
[0] => 1332594303
[1] => 1330960502
[2] => 1331227649
[3] => 1331305503
[4] => 1332594303
[5] => 1331147102
[6] => 1332680702
[7] => 1331301902
[8] => 1331048163
[9] => 1332248704
[10] => 1332421503
[11] => 31536000
[12] => 1331816703
[13] => 604800
[14] => 0
[15] => 31536000
[16] => 1332248703
[17] => 31536000
[18] => 1361922903
)
This is the script:
$k=0
$l=0
foreach ($qucls as $dt[$k]){
if (strlen($dt[$k]) < 10)
$dt[$k++] = '0';
else {$dt[$k++] = date("Ymd", $dt[$l++]);
}
}
for ($l=0; $l < $k; $l++){
}
This is the outcome after the loop:
Array
(
[0] => 20120324
[1] => 20120305
[2] => 20120308
[3] => 20120309
[4] => 20120324
[5] => 20120307
[6] => 20120325
[7] => 20120309
[8] => 20120306
[9] => 20120320
[10] => 20120322
[11] => 0
[12] => 19700101
[13] => 0
[14] => 0
[15] => 0
[16] => 19700817
[17] => 0
[18] => 19700101
)
Notice the date form is formatted properly until it reaches the 1st integer that is strlen < 10. At that point, it changes the less than 10 length integer to "0" which is correct, but the dates after that are goofed up. It continues to change the < 10 digit ones to 0 correctly.
Can someone help me figure out what is wrong in this loop? I'm not quite getting the right outcome with all those 1970 dates after the ELSE kicks in. I'm still new at this.
Thank you.
Use the below script
<?php
$qucls = array(
0 => 1332594303,
1 => 1330960502,
2 => 1331227649,
3 => 1331305503,
4 => 1332594303,
5 => 1331147102,
6=> 1332680702,
7=> 1331301902,
8=> 1331048163,
9=> 1332248704,
10 => 1332421503,
11 => 31536000,
12 => 1331816703,
13 => 604800,
14 => 0,
15 => 31536000,
16 => 1332248703,
17 => 31536000,
18 => 1361922903
);
foreach ($qucls as $key=>$value){
if (strlen($value)< 10){
$dt[] = 0;
}else{
$dt[] = date("Ymd", $value);
}
}
echo "<pre>";
print_r($array);
print_r($dt);
exit;
?>
and you will get the below output
Array
(
[0] => 20120324
[1] => 20120305
[2] => 20120308
[3] => 20120309
[4] => 20120324
[5] => 20120307
[6] => 20120325
[7] => 20120309
[8] => 20120306
[9] => 20120320
[10] => 20120322
[11] => 0
[12] => 20120315
[13] => 0
[14] => 0
[15] => 0
[16] => 20120320
[17] => 0
[18] => 20130226
)
Related
CSV:
0021044-1;16/02/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021044-2;16/02/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021064-1;21/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021064-1;21/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021067-1;19/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021087-1;14/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021087-2;14/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
I'm splitting the array with the following PHP code:
$csv = array_map(function ($v) {
return str_getcsv($v, ";");
}, file($file);
// Where file stands for the csv file being loaded.
Which gives me the following array
Array
(
[0] => Array
(
[0] => 0021044-1
[1] => 16/02/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[1] => Array
(
[0] => 0021044-2
[1] => 16/02/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[2] => Array
(
[0] => 0021064-1
[1] => 21/01/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[3] => Array
(
[0] => 0021064-1
[1] => 21/01/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[4] => Array
(
[0] => 0021067-1
[1] => 19/01/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[5] => Array
(
[0] => 0021087-1
[1] => 14/01/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[6] => Array
(
[0] => 0021087-2
[1] => 14/01/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
)
However, what I want to do is get the lines with the same date and perform a function on them
e.g;
Put the values
0021044-1;16/02/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021044-2;16/02/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
Then do a function on this array
0021064-1;21/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021064-1;21/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
Then do same function on this array
0021067-1;19/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
Then do same function on this array
0021087-1;14/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021087-2;14/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
Then do same function on this array
So in short, the function should be applied to a newly generated array out of this one, array as follows:
$new_array = Array
(
[0] => Array
(
[0] => 0021044-1
[1] => 16/02/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
[1] => Array
(
[0] => 0021044-2
[1] => 16/02/2022
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
[8] => 7
[9] => 8
[10] => 9
[11] => 10
[12] => 11
[13] => 12
[14] => 13
[15] => 14
[16] => 15
)
do the function csv2xml($new_array);
And then the second find of the other date etc...
do the function again etc..
All the sub arrays created on their appropriate date will then go to a function which I have covered already in a function called csv2xml($arr)
Though, I'm not succeeding in splitting the array into array's per date.
Can someone guide me in the correct direction?
I think it's a lot of for, while and loops nested in eachother but my brain is currently melting on this..
If the rows will always have the 2 dates following each other you can simply walk through the file calling your function on every other line, with a little check for those dates where only one line exists
I made up a little function to mimic your call that just prints the 2 dates to make sure it works.
function call_function($a1, $a2)
{
echo sprintf( "The 2 dates are %s and %s\n", $a1[1] , $a2[1]);
}
$f = fopen('tst.csv', 'r');
$last_date = NULL;
$last_line = NULL;
while ( ($line = fgetcsv($f, 1024, ';')) !== FALSE){
if ( $line[1] == $last_date ){
// we got the second of a set of dates so call your function
call_function($last_line, $line);
} else {
$last_date = $line[1];
$last_line = $line;
}
}
RESULTS
The 2 dates are 16/02/2022 and 16/02/2022
The 2 dates are 21/01/2022 and 21/01/2022
The 2 dates are 14/01/2022 and 14/01/2022
i have following array format in php:
Array
(
[item] => Array
(
[0] => RSJ Pole 116 x100 ,11 Mtr
[1] => RSJ Pole 116 x100 ,10 Mtr
[2] => RSJ Pole 116 x 100, 9 Mtr
[3] => RSJ Pole 116 x 100, 8 Mtr
[4] => Line Length in Kms
[5] => RSJ 125x70, 9 m long
[6] => PSC Pole 9 Mtr
[7] => PSC Pole 8 Mtr
[8] => Conductor ACSR Dog 0.1
[9] => Conductor ACSR Rabbit 55 Sq.mm
[10] => Conductor ACSR Raccon 80 Sq.mm
[11] => Conductor Weisel 34 Sq.mm
[12] => Conductor Ant
[13] => Conductor Gnat
[14] => New DTC 100 KVA
[15] => New DTC 63 KVA
[16] => BPL Connection
[17] => SDT
)
[scope] => Array
(
[0] => 1172
[1] => 6637
[2] => 854
[3] => 4
[4] => 653.71
[5] => 558.9
[6] => 5864
[7] => 820
[8] => 745385
[9] => 1188772.2
[10] => 327600
[11] => 18900
[12] => 232015.9
[13] => 70634.3
[14] => 344
[15] => 54
[16] => 13632
[17] => 37
)
[till] => Array
(
[0] => 3
[1] => 4
[2] => 4
[3] => 6
[4] => 5
[5] => 5
[6] => 5
[7] => 5
[8] => 2
[9] => 5
[10] => 5
[11] => 5
[12] => 5
[13] => 5
[14] => 5
[15] => 5
[16] => 5
[17] => 2
)
[value] => Array
(
[0] => 4
[1] => 7
[2] => 1
[3] => 2
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] =>
[17] =>
)
[submit] => submit
)
i want to concatenate with "_" operator before submitting or while inserting into database field for eg. 1172_3_4
You can array_map the 3 arrays
$arr = .... //Your array here
$results = array_map(function($s, $t, $v) {
return $s . "_" . $t . "_" . $v;
}, $arr['scope'], $arr['till'], $arr['value']);
echo "<pre>";
print_r( $results );
echo "</pre>";
$results will have the value:
Array
(
[0] => 1172_3_4
[1] => 6637_4_7
[2] => 854_4_1
[3] => 4_6_2
[4] => 653.71_5_
[5] => 558.9_5_
[6] => 5864_5_
[7] => 820_5_
[8] => 745385_2_
[9] => 1188772.2_5_
[10] => 327600_5_
[11] => 18900_5_
[12] => 232015.9_5_
[13] => 70634.3_5_
[14] => 344_5_
[15] => 54_5_
[16] => 13632_5_
[17] => 37_2_
)
Doc: array_map()
Implement the loop at main array and increment in $i then concatenate $array_variable['scope'][$i].'_'.$array_variable['till'][$i].'_'.$array_variable['value'][$i].
I want to parse the following excel file using PHP Excel Library and put the data into email templates(PFA).
Here goes the corresponding code for parsing the data:
// Read your Excel workbook
try {
$inputFileType = PHPExcel_IOFactory::identify($target_file);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($target_file);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($target_file,PATHINFO_BASENAME).'": '.$e->getMessage());
}
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){
$val=array();
for ($col = 0; $col < $highestColumn; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getValue();
}
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
// Insert row data array into your database of choice here
print_r(array_values($rowData));
The output is:
Array ( [0] => Array ( [0] => S No [1] => Hotel Name [2] => Room Category [3] => GuestHouse Id [4] => Hotel Email [5] => City [6] => state [7] => 2nd Feb [8] => 3rd Feb [9] => 4th Feb [10] => 5th Feb [11] => 6th Feb [12] => 7th Feb [13] => 8th Feb [14] => 9th Feb [15] => 10th Feb [16] => 11th Feb [17] => 12th Feb [18] => 13th Feb [19] => 14th Feb [20] => 15th Feb ) ) Array ( [0] => Array ( [0] => 1 [1] => Hotel 1 [2] => All [3] => 19 [4] => a#y.com [5] => Ahmedabad [6] => Gujarat [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15] => 0 [16] => 0 [17] => 0 [18] => 0 [19] => 0 [20] => 0 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Standard [3] => [4] => [5] => [6] => [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15] => 0 [16] => 0 [17] => 0 [18] => 0 [19] => 0 [20] => 0 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Superior [3] => [4] => [5] => [6] => [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15] => 0 [16] => 0 [17] => 0 [18] => 0 [19] => 0 [20] => 0 ) ) Array ( [0] => Array ( [0] => 2 [1] => Hotel 2 [2] => All [3] => 18 [4] => y#a.in [5] => Mumbai [6] => Maharashtra [7] => 2 [8] => 3 [9] => 4 [10] => 4 [11] => 4 [12] => 4 [13] => 4 [14] => 4 [15] => 4 [16] => 4 [17] => 4 [18] => 4 [19] => 2 [20] => 3 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Standard [3] => [4] => [5] => [6] => [7] => 0 [8] => 1 [9] => 2 [10] => 2 [11] => 2 [12] => 2 [13] => 2 [14] => 2 [15] => 2 [16] => 2 [17] => 2 [18] => 2 [19] => 0 [20] => 1 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Superior [3] => [4] => [5] => [6] => [7] => 2 [8] => 2 [9] => 2 [10] => 2 [11] => 2 [12] => 2 [13] => 2 [14] => 2 [15] => 2 [16] => 2 [17] => 2 [18] => 2 [19] => 2 [20] => 2 ) ) Array ( [0] => Array ( [0] => 3 [1] => Hotel 3 [2] => All [3] => 199 [4] => m#mm.co.in [5] => Amravati [6] => Maharashtra [7] => 20 [8] => 20 [9] => 20 [10] => 20 [11] => 20 [12] => 20 [13] => 20 [14] => 20 [15] => 20 [16] => 20 [17] => 20 [18] => 20 [19] => 20 [20] => 20 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Standard [3] => [4] => [5] => [6] => [7] => 10 [8] => 10 [9] => 10 [10] => 10 [11] => 10 [12] => 10 [13] => 10 [14] => 10 [15] => 10 [16] => 10 [17] => 10 [18] => 10 [19] => 10 [20] => 10 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Superior [3] => [4] => [5] => [6] => [7] => 10 [8] => 10 [9] => 10 [10] => 10 [11] => 10 [12] => 10 [13] => 10 [14] => 10 [15] => 10 [16] => 10 [17] => 10 [18] => 10 [19] => 10 [20] => 10 ) ) Array ( [0] => Array ( [0] => 4 [1] => Hotel 4 [2] => All [3] => 35 [4] => a#c.net [5] => Lonavala [6] => Maharashtra [7] => 1 [8] => 1 [9] => 1 [10] => 1 [11] => 1 [12] => 1 [13] => 1 [14] => 1 [15] => 1 [16] => 1 [17] => 1 [18] => 1 [19] => 1 [20] => 1 ) ) Array ( [0] => Array ( [0] => [1] => [2] => Villa [3] => [4] => [5] => [6] => [7] => 1 [8] => 1 [9] => 1 [10] => 1 [11] => 1 [12] => 1 [13] => 1 [14] => 1 [15] => 1 [16] => 1 [17] => 1 [18] => 1 [19] => 1 [20] => 1 ) )
Now, what I want is after I have have this multi-dimensional associative array in place, I want to extract the data in such a way so as to get this expected output.
This is the place where I am stuck. I am unable to extract data properly because on one hand I need to skip empty null fields whereas on the other hand I need to skip the "All" field. Any help or suggestion is highly appreciated.
I have an array like thus i want to sort this array date-wise how to sort the this array
Array
(
[0] => 28/02/2016
[1] => 30/01/2016
[2] => 16/02/2016
[3] => 19/02/2016
[4] => 24/02/2016
[5] => 13/02/2016
[6] => 18/02/2016
[7] => 27/02/2016
[8] => 25/02/2016
[9] => 01/02/2016
[10] => 02/02/2016
[11] => 03/02/2016
[12] => 05/02/2016
[13] => 06/02/2016
[14] => 07/02/2016
[15] => 08/02/2016
[16] => 11/02/2016
[17] => 12/02/2016
)
I have used usort but it does not work in which i am wirting a function and and converting it to strtotime but it does not work .Any suggestion.
Try below solution:
$array = array
(
0 => '28/02/2016',
1 => '30/01/2016',
2 => '16/02/2016',
3 => '19/02/2016',
4 => '24/02/2016',
5 => '13/02/2016',
6 => '18/02/2016',
7 => '27/02/2016',
8 => '25/02/2016',
9 => '01/02/2016',
10 => '02/02/2016',
11 => '03/02/2016',
12 => '05/02/2016',
13 => '06/02/2016',
14 => '07/02/2016',
15 => '08/02/2016',
16 => '11/02/2016',
17 => '12/02/2016'
);
function sortFunction( $a, $b ) {
$date1 = DateTime::createFromFormat('d/m/Y', $a);
$date2 = DateTime::createFromFormat('d/m/Y', $b);
return $date1->getTimestamp() - $date2->getTimestamp();
}
usort($array, "sortFunction");
print_r($array);
output
Array
(
[0] => 30/01/2016
[1] => 01/02/2016
[2] => 02/02/2016
[3] => 03/02/2016
[4] => 05/02/2016
[5] => 06/02/2016
[6] => 07/02/2016
[7] => 08/02/2016
[8] => 11/02/2016
[9] => 12/02/2016
[10] => 13/02/2016
[11] => 16/02/2016
[12] => 18/02/2016
[13] => 19/02/2016
[14] => 24/02/2016
[15] => 25/02/2016
[16] => 27/02/2016
[17] => 28/02/2016
)
Try this using sort()
$array = array
(
0 => '28/02/2016',
1 => '30/01/2016',
2 => '16/02/2016',
3 => '19/02/2016',
4 => '24/02/2016',
5 => '13/02/2016',
6 => '18/02/2016',
7 => '27/02/2016',
8 => '25/02/2016',
9 => '01/02/2016',
10 => '02/02/2016',
11 => '03/02/2016',
12 => '05/02/2016',
13 => '06/02/2016',
14 => '07/02/2016',
15 => '08/02/2016',
16 => '11/02/2016',
17 => '12/02/2016'
);
foreach($array as $key=>$val) {
$date_arr=explode('/',$val);
$time_arr[$key]=strtotime($date_arr[2].'/'.$date_arr[1].'/'.$date_arr[0]);
}
sort($time_arr);
foreach($time_arr as $key=>$val) {
$array[$key]=date("d/m/Y", $val);
}
echo '<pre>'; print_r($array); echo '</pre>';
Output
Array
(
[0] => 30/01/2016
[1] => 01/02/2016
[2] => 02/02/2016
[3] => 03/02/2016
[4] => 05/02/2016
[5] => 06/02/2016
[6] => 07/02/2016
[7] => 08/02/2016
[8] => 11/02/2016
[9] => 12/02/2016
[10] => 13/02/2016
[11] => 16/02/2016
[12] => 18/02/2016
[13] => 19/02/2016
[14] => 24/02/2016
[15] => 25/02/2016
[16] => 27/02/2016
[17] => 28/02/2016
)
I have an associative array in php.
the content of associative array looks like this:
Array
(
[0] => Array
(
[0] => 3
[1] => 1
[2] => 0
[3] => 50074494
[4] => 25013372
[5] => 2
[6] => 474
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 985
[12] => 34951
[13] => 18143
[14] => 4
[15] => 2
[16] => 94
[17] => 1
[18] => 1.26
[19] => 7.9
[20] => 2013-06-27 10:19:21
)
[1] => Array
(
[0] => 5
[1] => 1
[2] => 0
[3] => 50078122
[4] => 25000164
[5] => 2
[6] => 463
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 860
[12] => 28290
[13] => 16944
[14] => 4
[15] => 1
[16] => 94
[17] => 1
[18] => 1.13
[19] => 7.1
[20] => 2013-06-27 10:19:51
)
[2] => Array
(
[0] => 4
[1] => 1
[2] => 0
[3] => 50078630
[4] => 24995538
[5] => 2
[6] => 155
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 616
[12] => 23203
[13] => 4892
[14] => 3
[15] => 1
[16] => 95
[17] => 0
[18] => 1.04
[19] => 6.5
[20] => 2013-06-27 10:20:21
)
)
I would like to be able to assign the inner array values to a variable. I need variable to look like this:
echo $variable
3 1 0 50074494 25013372 2 474 .. 2013-06-27 10:19:21
.
.
I have this code so far:
$variable;
foreach ($lines as $key => $value) {
foreach ($value as &$val)
{
$variable=$variable . $val . ' ';
}
echo $variable;
echo "\n";
}
with this code it looks like I am getting 3 times of variable. Any ideas what I am doing wrong here?
If you have an array, and you want to store the values in a space-separated string, you could do this:
$string = implode(' ', $array);
echo $string;
So your loop might look like this:
foreach ($lines as $value) {
$value[20] = '"'. $value[20] .'"'; // from comments
echo implode(' ', $value) ."\n";
}
I would recommend the use of implode instead of foreach. Also, it doesn't seem like you need the value of $key:
foreach ($lines as $value) {
echo implode(" ", $value);
echo "\n";
}
Also, I'm not sure I quite understood your question? What do you mean by "3 times of variable"?