php associative array error - arrays

Please can someone tell me what I am doing wrong here because it is giving me a blank result. just a newbie wanting to learn.
$months = "2";
$month = array(1=>January,"2"=>February,"3"=>March,"4"=>April,"5"=>May,"6"=>June,"7"=>July,"8"=>August,"9"=>September,"10"=>October,"11"=>November,"12"=>December);
$description = 'In respect of '.$particular.' collection for the month of ';print $month['$months'];
echo $description

You haven't enclosed the array string values in quotes. Change your $month definition to this:
$month = array(
1 => "January",
2 => "February",
3 => "March",
4 => "April",
5 => "May",
6 => "June",
7 => "July",
8 => "August",
9 => "September",
10 => "October",
11 => "November",
12 => "December"
);
Also, you don't really need to create an associative array for the month names. You can get a month's name from its number like this:
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));

Related

I want to convert month number to month name in ReactJs

This is the data in json file. the _id i am passing is month number.
Const incomes = [
{
"_id": 6,
"total": 3000
},
{
"_id": 7,
"total": 3500
}
]
I am trying to convert the month number to month name by using this function. but its not working.
const monthNames = ["January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
const data = useMemo(
() => incomes.map((income, monthNames) => ({ ...income, name: monthNames[income._id.getMonth()] }
)
), [incomes, monthNames]);
Try this!
I think when you passed the monthNames in the .map it declared it as a new variable and that's why the result was undefined.
P.S. Added -1 to income._id so the months would match
Hope it helps :)
const incomes = [
{
"_id": 6,
"total": 3000
},
{
"_id": 7,
"total": 3500
}
]
const monthNames = ["January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
const data = useMemo(
() => incomes.map((income) => ({ ...income, name: monthNames[income._id -1]}
)
), [incomes, monthNames]);

How to properly initialize array of structures inside another structure and return it from function?

I have an array of such structures:
typedef struct month {
char cName[20];
char cAbbr[20];
int iDays;
int iNumber;
} MONTH;
which is nested in another structure:
typedef struct input{
MONTH * pMonths;
DATE sDate;
CALCS sCalcs;
} INPUT;
I initialize this array of structs in stack like that:
MONTH * init_months(bool bLeap) {
MONTH months[NUM_MONTHS] = {
{ .cName = "January", .cAbbr = "Jan", .iDays = 31, .iNumber = 1 },
{ "February", "Feb", 28, 2 },
{ "March", "Mar", 31, 3 },
{ "April", "Apr", 30, 4 },
{ "May", "May", 31, 5 },
{ "June", "Jun", 30, 6 },
{ "July", "Jul", 31, 7 },
{ "August", "Aug", 31, 8 },
{ "September", "Sep", 30, 9 },
{ "October", "Oct", 31, 10 },
{ "November", "Nov", 30, 11 },
{ "December", "Dec", 31, 12 }
};
if(bLeap){
months[1].iDays++;
}
return months;
}
The question is, if i form a INPUT structure inside some function:
INPUT GetUserInput(void){
// init months
MONTH * pMonths;
pMonths = init_months(isLeap(sDate.iYear));
// some code here
...
INPUT sInput = { pMonths, sDate, sCalcs };
// return
return sInput;
}
then how to properly initialize array of MONTH and/or how to properly initialize INPUT so it would contain valid data when returned from GetUserInput() ?
You have 3 options:
Make it static (but all returned references will reference the same array).
static MONTH months[NUM_MONTHS] = {
Use malloc and compound literal
MONTH *months = malloc(sizeof(*months) * NUM_MONTHS);
/* check if memory was allocated */
memcpy(months, (MONTH[]){
{ .cName = "January", .cAbbr = "Jan", .iDays = 31, .iNumber = 1 },
{ "February", "Feb", 28, 2 },
{ "March", "Mar", 31, 3 },
{ "April", "Apr", 30, 4 },
{ "May", "May", 31, 5 },
{ "June", "Jun", 30, 6 },
{ "July", "Jul", 31, 7 },
{ "August", "Aug", 31, 8 },
{ "September", "Sep", 30, 9 },
{ "October", "Oct", 31, 10 },
{ "November", "Nov", 30, 11 },
{ "December", "Dec", 31, 12 }}, sizeof(*months) * NUM_MONTHS);
Wrap the table into another structure and return by value
typedef struct
{
MONTH months[NUM_MONTHS];
}YEAR;
YEAR init_months(bool bLeap) {
YEAR year = {{
{ .cName = "January", .cAbbr = "Jan", .iDays = 31, .iNumber = 1 },
{ "February", "Feb", 28, 2 },
{ "March", "Mar", 31, 3 },
{ "April", "Apr", 30, 4 },
{ "May", "May", 31, 5 },
{ "June", "Jun", 30, 6 },
{ "July", "Jul", 31, 7 },
{ "August", "Aug", 31, 8 },
{ "September", "Sep", 30, 9 },
{ "October", "Oct", 31, 10 },
{ "November", "Nov", 30, 11 },
{ "December", "Dec", 31, 12 }}};
if(bLeap){
year.months[1].iDays++;
}
return year;
}

Split an array in freemarker template into multiple arrays

I have an array, is it possible to split this array into sub arrays using freemarker.
Freemaker template how to split a existing array into sub array
<#assign monthsOfTheYear = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] />
"January", "February", "March", "April" and "October", "November", "December"
"January", "February", "March" - should be in 1 array and .
"October", "November", "December" -- Should be 2nd array
Yes its possible to dynamically split the array into sub arrays one of the method is as follows.
The following is an example of it
<#assign first = monthsOfTheYear[1..4] />
<#assign second = monthsOfTheYear[6..9] />
If its a string you will have to probably convert into a number, assuming startdate and enddate are string with value say 02 and 04
<#assign sublistVar = monthsOfTheYear[(startdate?number-1)..(enddate?number-1)] />

How to get month name from month number

month=[1,2,3,4,5,6,7,8,9]
This array I am getting from server. How to get Month Name?
Till now I tried this:
var populateChartData = function (data) {
var getMonth = [];
var getProfit = [];
$scope.chartTitle = 'Year :' + moment().year();
data.forEach(function (obj) {
console.log(obj.month);
getMonth.push(moment().month(obj.month).format('MMM'));
});
console.log(getMonth);
};
but in console I'm getting
["Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct"]
I should get like this
["Jan","Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep"]
you could just create your own filter like this: otherwise, pass the index into one function and get Month Name
myApp.filter('monthName', [function() {
return function (monthNumber) { //1 = January
var monthNames = [ 'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December' ];
return monthNames[monthNumber - 1];
}
}]);
0 corresponds to January, 1 to February and so on (from 0 to 11). It is done in such way because indexing usually starts from 0. Hence from programming point of view 0 is a good choice for January
In momentjs month number starts from 0.
You can see this documentation from moment
momentjs month
I think you should try from 0 index in the month array.or while iterating that array put -1 for that value.
I did as below
var populateChartData = function (data) {
var getMonth = [];
var getProfit = [];
data.forEach(function (obj) {
getMonth.push(moment().month(obj.month-1).format('MMM'));
});
console.log(getMonth);
};

How to pre load all locales into angular dynamic locale cache (tmhDynamicLocaleCache)

I am using angular dynamic locale (tmhDynamicLocale), but want all my locales to be pre loaded into the cache (tmhDynamicLocaleCache) when the app starts. The motivation behind this is to have all my resources bundled into 1 minified js file rather than the standard lazy loading. Is there a full example for this anywhere?
Solved it.
What i did was to put each locale into a module like so:
angular.module("myapp.locale.en", [
'tmh.dynamicLocale'
])
.run(['tmhDynamicLocaleCache', function (tmhDynamicLocaleCache) {
var PLURAL_CATEGORY = {ZERO: "zero", ONE: "one", TWO: "two", FEW: "few", MANY: "many", OTHER: "other"};
function getDecimals(n) {
n = n + '';
var i = n.indexOf('.');
return (i == -1) ? 0 : n.length - i - 1;
};
function getVF(n, opt_precision) {
var v = opt_precision;
if (undefined === v) {
v = Math.min(getDecimals(n), 3);
}
var base = Math.pow(10, v);
var f = ((n * base) | 0) % base;
return {v: v, f: f};
};
tmhDynamicLocaleCache.put('en', {
"DATETIME_FORMATS": {
"AMPMS": [
"AM",
"PM"
],
"DAY": [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
],
"FIRSTDAYOFWEEK": 6,
"MONTH": [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
"SHORTDAY": [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
],
"SHORTMONTH": [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
],
"WEEKENDRANGE": [
5,
6
],
"fullDate": "EEEE, MMMM d, y",
"longDate": "MMMM d, y",
"medium": "MMM d, y h:mm:ss a",
"mediumDate": "MMM d, y",
"mediumTime": "h:mm:ss a",
"short": "M/d/yy h:mm a",
"shortDate": "M/d/yy",
"shortTime": "h:mm a"
},
"NUMBER_FORMATS": {
"CURRENCY_SYM": "$",
"DECIMAL_SEP": ".",
"GROUP_SEP": ",",
"PATTERNS": [
{
"gSize": 3,
"lgSize": 3,
"maxFrac": 3,
"minFrac": 0,
"minInt": 1,
"negPre": "-",
"negSuf": "",
"posPre": "",
"posSuf": ""
},
{
"gSize": 3,
"lgSize": 3,
"maxFrac": 2,
"minFrac": 2,
"minInt": 1,
"negPre": "\u00a4-",
"negSuf": "",
"posPre": "\u00a4",
"posSuf": ""
}
]
},
"id": "en",
"pluralCat": function (n, opt_precision) {
var i = n | 0;
var vf = getVF(n, opt_precision);
if (i == 1 && vf.v === 0) {
return PLURAL_CATEGORY.ONE;
}
return PLURAL_CATEGORY.OTHER;
}
});
}])
;
Create a module like this for each locale you use, just change "en" to "pt" or "he" etc (3 places in the code above).
Next load these modules into your app.js...
angular.module('myapp', [
'myapp.locale.en',
'myapp.locale.pt',
'myapp.locale.de',
'myapp.locale.he'
])
This way when .run() is called on each of these modules it will put the locale object into the tmhDynamicLocaleCache. Then when you later use tmhDynamicLocale.set('en') it will find the locale object in the cache and use it.

Resources