I am working in PHP. I want to get my timezone, Asia/Dhaka, from offset 6x60x60 = 21600. I have tried with timezone_name_from_abbr("", 21600, false);
But it's not working. I'm getting false.
What can I do now?
The official documentation mentions a bug in that function : PHP timezone_name_from_abbr
You can write a function to parse the result of timezone_abbreviations_list() yourself :
/* Takes a GMT offset and returns a timezone name */
function tz_offset_to_name($offset, $dst)
{
$results = array();
$abbrarray = timezone_abbreviations_list();
foreach ($abbrarray as $abbr)
{
foreach ($abbr as $city)
{
if ($city['offset'] == $offset and $city['dst'] == $dst)
{
$results[] = $city['timezone_id'];
}
}
}
return $results;
}
It gives the correct result :
$offset = 6*60*60 ;
$tz = timezone_name_from_abbr("", $offset, false);
var_dump($tz); // false
$tz = tz_offset_to_name($offset, false);
print_r($tz);
(
[0] => Asia/Aqtobe
[1] => Asia/Almaty
[2] => Asia/Dacca
[3] => Asia/Dhaka
[4] => Asia/Thimbu
[5] => Asia/Thimphu
[6] => Asia/Dacca
[7] => Asia/Dhaka
[8] => Asia/Dushanbe
[9] => Asia/Bishkek
[10] => Asia/Hovd
[11] => Indian/Chagos
[12] => Asia/Bishkek
[13] => Asia/Qyzylorda
[14] => Asia/Krasnoyarsk
[15] => Asia/Novokuznetsk
[16] => Asia/Colombo
[17] => Antarctica/Mawson
[18] => Asia/Novosibirsk
[19] => Asia/Novokuznetsk
[20] => Asia/Omsk
[21] => Asia/Qyzylorda
[22] => Asia/Aqtau
[23] => Asia/Samarkand
[24] => Asia/Tashkent
[25] => Asia/Oral
[26] => Antarctica/Vostok
[27] => Asia/Kashgar
[28] => Asia/Urumqi
[29] => Asia/Yekaterinburg
[30] =>
)
Using xPath query, I am trying to extract HTML values and put them into an associative array in PHP. I am using a loop to get the rows and cells from the table. But, I can;t figure out how to get the cells in an array embedded within an array that represents the row. Basically, just transferring the table structure to an array. Ideally, it would help to assign keys for the cell data.
I tried combining $key as an array and a counter to assign key/value pair. I and xpath at different points of the structure. I can get fill up the array but I am But I just can't seem to crack it.
$cells = array();
$cell_values = array();
$key = array("MM", "DD", "TIME", "WVHT", "SwH", "SwP", "SwD", "WWH", "WWP", "WWD", "STEEPNESS", "APD");
$i = 3;
while($i <= 5){
$rows = $xpath->query('//table[#class="dataTable"][2]/tr['.$i.']');
if (!is_null($rows)){
foreach ($rows as $row) {
$cells = $row->getElementsByTagName('td');
$i++;
foreach ($cells as $cell) {
$cell_values[] = $cell->nodeValue;
$dataOut[] = array_combine($key, $cell_values);
}
}
}
}
Expected:
Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9]
=> [10] =>
[11] => Array ( [MM] => 02 [DD] => 17 [TIME] => 11:30 am [WVHT]
=> 3.0 [SwH] => 0.3 [SwP] => 10.5 [SwD] => SE [WWH] => 2.6 [WWP] => 8.3
[WWD] => SE [STEEPNESS] => AVERAGE [APD] => 4.4 )
//Next set of row data with $keys
[12] => Array ( [MM] => 02 [DD] => 17 [TIME] => 11:00 am [WVHT] => 3.3 [SwH]
=> 0.3 [SwP] => 10.5 [SwD] => SE [WWH] => 2.6 [WWP] => 8.3 [WWD] => SE
[STEEPNESS] => AVERAGE [APD] => 4.4 )
[13] => Array... etc.
What I Get:
Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9]
=> [10] => [11] => Array ( [MM] => 02 [DD] => 17 [TIME] => 11:30 am [WVHT]
=> 3.0 [SwH] => 0.3 [SwP] => 10.5 [SwD] => SE [WWH] => 2.6 [WWP] => 8.3
[WWD] => SE [STEEPNESS] => AVERAGE [APD] => 4.4 ) [12] => [13] => [14] =>
[15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24]
=> [25] => [26] => [27] => [28] => [29] => [30] => [31] => [32] => [33] =>
[34] => [35] => )
If you define your keys as values in the $key array rather than keys, you can array_combine that with the values from the <td>s to produce the rows for your result array.
$rows = $xpath->query('//table[#class="dataTable"][2]/tr');
// define these as values so you can use them in array_combine
$key = array("WVHT", "SwH", "SwD", "WWH", "WWP", "WWD", "STEEPNESS", "AMD");
$data = array();
$cells = array();
if (!is_null($rows)) {
foreach ($rows as $row) {
$cells = $row->getElementsByTagName('td');
// get all the cell values from the row
$row_values = [];
foreach ($cells as $cell) {
$row_values[] = $cell->nodeValue;
}
// combine the keys with the values for this row and add the row to $data
$data[] = array_combine($key, $row_values);
}
}
I have two arrays:
1- Id person (key) and qualification (value), this array have a descending order : arsort
Array
(
[61] => 02.30.00
[95] => 02.30.00
[19] => 02.01.00
[131] => 02.00.00
[58] => 01.60.00
[97] => 01.50.00
[76] => 01.40.00
[20] => 01.30.00
[112] => 01.10.00
[42] => 01.10.00
[116] => 01.04.00
}
2- ... and attempts associated to the Id person.
Array
(
[131] => 1
[58] => 1
[61] => 1
[112] => 2
[116] => 1
[42] => 1
[19] => 1
[20] => 1
[76] => 1
[97] => 1
[95] => 1
)
I need to maintain the descending order but adding ascending order by the number of the attempts. My problem is with these values:
[112] => 01.10.00 | 2
[42] => 01.10.00 | 1
How get this result?
Array
(
[61] => 02.30.00 // 1
[95] => 02.30.00 // 1
[19] => 02.01.00 // 1
[131] => 02.00.00 // 1
[58] => 01.60.00 // 1
[97] => 01.50.00 // 1
[76] => 01.40.00 // 1
[20] => 01.30.00 // 1
[42] => 01.10.00 // 1
[112] => 01.10.00 // 2
[116] => 01.04.00 // 1
)
Edit: ugly solution:
$new = array();
foreach($qualification as $k => $r)
{
$new[$k] = array(
'qualification'=> $r,
'attempt' => $attempt[$k],
'id' => $k,
);
}
foreach ($new as $key => $row)
{
$qlf[$key] = $row['qualification'];
$att[$key] = $row['attempt'];
}
array_multisort($qlf, SORT_DESC, $att, SORT_ASC, $new);
$result = array();
foreach ($new as $row)
{
$result[$row['id']] = $row['qualification'];
}
print_r($result);
uksort($qualification, function ($a, $b) use ($qualification, $attempt) {
return strcmp($qualification[$a], $qualification[$b])
?: $attempt[$a] - $attempt[$b];
});
I'm not entirely sure about which order you want to sort what in, but the above code will do; you just may have to switch $a and $b for reversing the order of one or the other.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have been coding and improving the code jszobody provided me that can found on this LINK which It makes me to be my refference but it results me to an error Fatal error: Using $this when not in object context
PHP:
<?php
function getAllPossiblePermutations($mdArray, $firstTime=true, $tempArray=array())
{
// initialize results array
if ($firstTime)
{
$this->permutationsResultsArray = array();
}
// find first sub array and iterate through it
$thisArray = array_shift($mdArray);
foreach ($thisArray as $key => $elem)
{
// if this number has already been used skip this possible permutation
if (in_array($elem, $tempArray))
{
continue;
}
$tempArray[] = $elem;
if (count($mdArray) == 0)
{
$this->permutationsResultsArray[] = $tempArray;
}
else
{
$this->getAllPossiblePermutations($mdArray, false, $tempArray);
}
array_pop($tempArray);
}
}
$traits = array
(
array('Happy', 'Sad', 'Angry', 'Hopeful'),
array('Outgoing', 'Introverted'),
array('Tall', 'Short', 'Medium'),
array('Handsome', 'Plain', 'Ugly')
);
print_r(getAllPossiblePermutations($traits));
?>
EXPECTED OUTPUT:
Array ( [0] => HappyOutgoingTallHandsome 1 => HappyOutgoingTallPlain [2] => HappyOutgoingTallUgly [3] => HappyOutgoingShortHandsome [4] => HappyOutgoingShortPlain [5] => HappyOutgoingShortUgly [6] => HappyOutgoingMediumHandsome [7] => HappyOutgoingMediumPlain [8] => HappyOutgoingMediumUgly [9] => HappyIntrovertedTallHandsome [10] => HappyIntrovertedTallPlain [11] => HappyIntrovertedTallUgly [12] => HappyIntrovertedShortHandsome [13] => HappyIntrovertedShortPlain [14] => HappyIntrovertedShortUgly [15] => HappyIntrovertedMediumHandsome [16] => HappyIntrovertedMediumPlain [17] => HappyIntrovertedMediumUgly [18] => SadOutgoingTallHandsome [19] => SadOutgoingTallPlain [20] => SadOutgoingTallUgly [21] => SadOutgoingShortHandsome [22] => SadOutgoingShortPlain [23] => SadOutgoingShortUgly [24] => SadOutgoingMediumHandsome [25] => SadOutgoingMediumPlain [26] => SadOutgoingMediumUgly [27] => SadIntrovertedTallHandsome [28] => SadIntrovertedTallPlain [29] => SadIntrovertedTallUgly [30] => SadIntrovertedShortHandsome [31] => SadIntrovertedShortPlain [32] => SadIntrovertedShortUgly [33] => SadIntrovertedMediumHandsome [34] => SadIntrovertedMediumPlain [35] => SadIntrovertedMediumUgly [36] => AngryOutgoingTallHandsome [37] => AngryOutgoingTallPlain [38] => AngryOutgoingTallUgly [39] => AngryOutgoingShortHandsome [40] => AngryOutgoingShortPlain [41] => AngryOutgoingShortUgly [42] => AngryOutgoingMediumHandsome [43] => AngryOutgoingMediumPlain [44] => AngryOutgoingMediumUgly [45] => AngryIntrovertedTallHandsome [46] => AngryIntrovertedTallPlain [47] => AngryIntrovertedTallUgly [48] => AngryIntrovertedShortHandsome [49] => AngryIntrovertedShortPlain [50] => AngryIntrovertedShortUgly [51] => AngryIntrovertedMediumHandsome [52] => AngryIntrovertedMediumPlain [53] => AngryIntrovertedMediumUgly [54] => HopefulOutgoingTallHandsome [55] => HopefulOutgoingTallPlain [56] => HopefulOutgoingTallUgly [57] => HopefulOutgoingShortHandsome [58] => HopefulOutgoingShortPlain [59] => HopefulOutgoingShortUgly [60] => HopefulOutgoingMediumHandsome [61] => HopefulOutgoingMediumPlain [62] => HopefulOutgoingMediumUgly [63] => HopefulIntrovertedTallHandsome [64] => HopefulIntrovertedTallPlain [65] => HopefulIntrovertedTallUgly [66] => HopefulIntrovertedShortHandsome [67] => HopefulIntrovertedShortPlain [68] => HopefulIntrovertedShortUgly [69] => HopefulIntrovertedMediumHandsome [70] => HopefulIntrovertedMediumPlain [71] => HopefulIntrovertedMediumUgly [72] => )
where did I go wrong?
You have a lot of referemces to class variables, like this:
$this->permutationsResultsArray = array();
And PHP complains since this function is not a method in a class. It will work if you just remove this-> so you get:
$permutationsResultsArray = array();
In addition when you are all done you never really return the result.. Like this:
return $permutationsResultsArray;
There is a problem with it though. You are recusing and you don't create that array except in the first round but your code uses it as if it was defined.
BTW: Your function could be much easier with 3 foreach loops:
function getCombinations($traits)
{
$combinations = array('');
foreach( $traits as $trait_level ) {
$new_combinations = array();
foreach ( $combinations as $comb ) {
foreach ( $trait_level as $trait ){
$new_combinations[] = "$comb $trait";
}
}
$combinations = $new_combinations;
}
return $combinations;
}
A function doesn't have a self referential $this. Actually, removing all your $this-> references and make $tempArray a pass by reference instead of by value, your code should work...
function getAllPossiblePermutations($mdArray, $firstTime=true, &$tempArray=array())
I am trying to insert an array into an array of arrays using array_splice.
array_splice($array, 0, 0, $fieldNames);
The resulting array is then converted to csv format with fputcsv. I get the following error:
fputcsv() expects parameter 2 to be array string given in...
What am I doing wrong? Must be some minor bug. Please any help. Also am I taking the right approach. My PHP knowledge of functions is not huge.
Full php code:
<?php
//MySQL login details
require('sqlAuth.php');
//return error data
function errorReport($error)
{
$data = array("error" => $error);
$json = json_encode($data);
echo $json;
die();
}
//export array to csv file
function outputCSV($data) {
$outstream = fopen("php://output", "w");
function __outputCSV(&$vals, $key, $filehandler) {
fputcsv($filehandler, $vals); // add parameters if you want
}
array_walk($data, "__outputCSV", $outstream);
fclose($outstream);
}
// open connection
$connection = mysql_connect($host, $user, $pass) or errorReport("Unable to Connect");
// select database
mysql_select_db($db, $connection) or errorReport("Unable to Select Database: " .mysql_error());
//build result set array
$array = array();
//get full questions table
$query = "SELECT q.q_id, q.trip_day, q.q0_1, q.q0_2, q.q0_3, q.q0_4, q.q0_5, q.q0_6, g.address, g.latitude, g.longitude, g.method, q.q1_1, q.q1_2, q.q1_3, q.q1_7, q.q1_9, q.q1_10, q.q1_11_1, q.q1_11_2, q.q1_11_3, q.q1_11_4, q.q2_6, q.q6_0,
q.q6_1, q.q6_1_1_1, q.q6_1_1_2, q.q6_1_1_3, q.q6_1_1_4, q.q6_1_1_5, q.q6_1_1_6, q.q6_1_1_7, q.q6_1_1_8, q.q6_1_1_9, q.q6_1_1_10, q.q6_1_1_11, q.q6_1_1_12, q.q6_1_1_13, q.q6_1_1_14,
q.q6_1_1_15, q.q6_1_1_16, q.q6_1_2_1, q.q6_1_2_2, q.q6_1_2_3, q.q6_1_2_4, q.q6_1_2_5, q.q6_1_2_6, q.q6_1_2_7, q.q6_1_2_8, q.q6_1_2_9, q.q6_1_2_10, q.q6_1_2_11, q.q6_1_2_12, q.q6_1_2_13,
q.q6_1_2_14, q.q6_1_2_15, q.q6_1_2_16, q.q6_1_3_1, q.q6_1_3_2, q.q6_2, q.q6_2_1_1, q.q6_2_1_2, q.q6_2_1_3, q.q6_2_1_4, q.q6_2_1_5, q.q6_2_1_6, q.q6_2_1_7, q.q6_2_1_8, q.q6_2_1_9,
q.q6_2_1_10, q.q6_2_1_11, q.q6_2_1_12, q.q6_2_1_13, q.q6_2_1_14, q.q6_2_1_15, q.q6_2_1_16, q.q6_2_2_1, q.q6_2_2_2, q.q6_2_2_3, q.q6_2_2_4, q.q6_2_2_5, q.q6_2_2_6, q.q6_2_2_7,
q.q6_2_2_8, q.q6_2_2_9, q.q6_2_2_10, q.q6_2_2_11, q.q6_2_2_12, q.q6_2_2_13, q.q6_2_2_14, q.q6_2_2_15, q.q6_2_2_16, q.q6_2_3_1, q.q6_2_3_2, q.q6_3, q.q6_3_1_1, q.q6_3_1_2,
q.q6_3_1_3, q.q6_3_1_4, q.q6_3_1_5, q.q6_3_1_6, q.q6_3_1_7, q.q6_3_1_8, q.q6_3_1_9, q.q6_3_1_10, q.q6_3_1_11, q.q6_3_1_12, q.q6_3_1_13, q.q6_3_1_14, q.q6_3_2_1, q.q6_3_2_2, q.q6_3_2_3,
q.q6_3_2_4, q.q6_3_2_5, q.q6_3_2_6, q.q6_3_2_7, q.q6_3_2_8, q.q6_3_2_9, q.q6_3_2_10, q.q6_3_2_11, q.q6_3_2_12, q.q6_3_2_13, q.q6_3_2_14, q.q6_3_3_1, q.q6_3_3_2
FROM questions AS q LEFT JOIN gmap_address_list AS g ON q.q0_7 = g.id";
$result_questions = mysql_query($query) or errorReport("Error in query: $query. ".mysql_error());
while ($row = mysql_fetch_array($result_questions, MYSQL_ASSOC)) {
$array[] = $row;
}
//get field names
$fieldNames = array();
$fieldNum = mysql_num_fields($result_questions);
for ($i = 0; $i < $fieldNum; $i++) {
$fieldNames[] = mysql_field_name($result_questions, $i);
}
array_splice($array, 0, 0, $fieldNames);
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=emmhts_questionnaires.csv");
header("Pragma: no-cache");
header("Expires: 0");
outputCSV($array);
// free result set memory
mysql_free_result($result_questions);
//close connection to mysql db
mysql_close($connection);
?>
Edit:
Here is a section of the array for what should be the first row of the csv file after adding print_r($array) just before outputCSV($array). It looks like the array inserted in position 0 is not closed after "[124] => q6_3_3_2", but rather there is a sub array.
Array
(
[0] => q_id
[1] => trip_day
[2] => q0_1
[3] => q0_2
[4] => q0_3
[5] => q0_4
[6] => q0_5
[7] => q0_6
[8] => address
[9] => latitude
[10] => longitude
[11] => method
[12] => q1_1
[13] => q1_2
[14] => q1_3
[15] => q1_7
[16] => q1_9
[17] => q1_10
[18] => q1_11_1
[19] => q1_11_2
[20] => q1_11_3
[21] => q1_11_4
[22] => q2_6
[23] => q6_0
[24] => q6_1
[25] => q6_1_1_1
[26] => q6_1_1_2
[27] => q6_1_1_3
[28] => q6_1_1_4
[29] => q6_1_1_5
[30] => q6_1_1_6
[31] => q6_1_1_7
[32] => q6_1_1_8
[33] => q6_1_1_9
[34] => q6_1_1_10
[35] => q6_1_1_11
[36] => q6_1_1_12
[37] => q6_1_1_13
[38] => q6_1_1_14
[39] => q6_1_1_15
[40] => q6_1_1_16
[41] => q6_1_2_1
[42] => q6_1_2_2
[43] => q6_1_2_3
[44] => q6_1_2_4
[45] => q6_1_2_5
[46] => q6_1_2_6
[47] => q6_1_2_7
[48] => q6_1_2_8
[49] => q6_1_2_9
[50] => q6_1_2_10
[51] => q6_1_2_11
[52] => q6_1_2_12
[53] => q6_1_2_13
[54] => q6_1_2_14
[55] => q6_1_2_15
[56] => q6_1_2_16
[57] => q6_1_3_1
[58] => q6_1_3_2
[59] => q6_2
[60] => q6_2_1_1
[61] => q6_2_1_2
[62] => q6_2_1_3
[63] => q6_2_1_4
[64] => q6_2_1_5
[65] => q6_2_1_6
[66] => q6_2_1_7
[67] => q6_2_1_8
[68] => q6_2_1_9
[69] => q6_2_1_10
[70] => q6_2_1_11
[71] => q6_2_1_12
[72] => q6_2_1_13
[73] => q6_2_1_14
[74] => q6_2_1_15
[75] => q6_2_1_16
[76] => q6_2_2_1
[77] => q6_2_2_2
[78] => q6_2_2_3
[79] => q6_2_2_4
[80] => q6_2_2_5
[81] => q6_2_2_6
[82] => q6_2_2_7
[83] => q6_2_2_8
[84] => q6_2_2_9
[85] => q6_2_2_10
[86] => q6_2_2_11
[87] => q6_2_2_12
[88] => q6_2_2_13
[89] => q6_2_2_14
[90] => q6_2_2_15
[91] => q6_2_2_16
[92] => q6_2_3_1
[93] => q6_2_3_2
[94] => q6_3
[95] => q6_3_1_1
[96] => q6_3_1_2
[97] => q6_3_1_3
[98] => q6_3_1_4
[99] => q6_3_1_5
[100] => q6_3_1_6
[101] => q6_3_1_7
[102] => q6_3_1_8
[103] => q6_3_1_9
[104] => q6_3_1_10
[105] => q6_3_1_11
[106] => q6_3_1_12
[107] => q6_3_1_13
[108] => q6_3_1_14
[109] => q6_3_2_1
[110] => q6_3_2_2
[111] => q6_3_2_3
[112] => q6_3_2_4
[113] => q6_3_2_5
[114] => q6_3_2_6
[115] => q6_3_2_7
[116] => q6_3_2_8
[117] => q6_3_2_9
[118] => q6_3_2_10
[119] => q6_3_2_11
[120] => q6_3_2_12
[121] => q6_3_2_13
[122] => q6_3_2_14
[123] => q6_3_3_1
[124] => q6_3_3_2
[125] => Array
(
[q_id] => 29
[trip_day] => Thursday
[q0_1] => 4
[q0_2] => 5
[q0_3] => 5
[q0_4] => 5
[q0_5] => 5
[q0_6] => 0732152589
Thanks for posting your array data. The problem is your array is multidimensional. I.e. you have a 'subarray' at 125.
Looking at the docs (http://php.net/manual/en/function.fputcsv.php) it isn't clear, but you can't use multidimensional arrays with fputcsv. If you think about it, the array you pass will be converted to one line in the csv.
You will need to think about the structure of your data, and how you expect it to be formatted in your csv, and modify your code accordingly.