By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,639 Members | 1,988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,639 IT Pros & Developers. It's quick & easy.

array of year/months

P: n/a
Hi all,

it's there any simple way to create an array of years and months dynamically
(2 loops) for an associative array ?
I've tried this so far: (having an error on last line with the "=>" sign.

$table = array();
for($i=2005;$i<=date('y');$i++){
$table = array_push($i=>array());
for($d=1;$d<=12;$d++)
$table[$i] = array_push($table,$i=>0);
}

What I need is to create a table wich will then be written and readed using
$table[$year][$month] = $somevalue;

Thanks for helping.
Jun 2 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Bob Bedford schreef:
Hi all,

it's there any simple way to create an array of years and months dynamically
(2 loops) for an associative array ?
I've tried this so far: (having an error on last line with the "=>" sign.

$table = array();
for($i=2005;$i<=date('y');$i++){
$table = array_push($i=>array());
for($d=1;$d<=12;$d++)
$table[$i] = array_push($table,$i=>0);
}

What I need is to create a table wich will then be written and readed using
$table[$year][$month] = $somevalue;

Thanks for helping.

Hi,

$table = array();
for($i=2005;$i<=date('y');$i++){
for($d=1;$d<=12;$d++)
$table[$i][$d] = "somevalue";
}

Something like that?

Regards,
Erwin Moller
Jun 2 '08 #2

P: n/a
Hi Erwin,

thanks for replying.

Reading is not a problem. I've a problem to "build" the associative array.

"Erwin Moller"
<Si******************************************@spam yourself.coma écrit dans
le message de news: 48***********************@news.xs4all.nl...
Bob Bedford schreef:
>Hi all,

it's there any simple way to create an array of years and months
dynamically (2 loops) for an associative array ?
I've tried this so far: (having an error on last line with the "=>" sign.

$table = array();
for($i=2005;$i<=date('y');$i++){
$table = array_push($i=>array());
for($d=1;$d<=12;$d++)
$table[$i] = array_push($table,$i=>0);
}

What I need is to create a table wich will then be written and readed
using $table[$year][$month] = $somevalue;

Thanks for helping.

Hi,

$table = array();
for($i=2005;$i<=date('y');$i++){
for($d=1;$d<=12;$d++)
$table[$i][$d] = "somevalue";
}

Something like that?

Regards,
Erwin Moller

Jun 2 '08 #3

P: n/a
Bob Bedford schreef:
Hi Erwin,

thanks for replying.

Reading is not a problem. I've a problem to "build" the associative array.

"Erwin Moller"
<Si******************************************@spam yourself.coma écrit dans
le message de news: 48***********************@news.xs4all.nl...
>Bob Bedford schreef:
>>Hi all,

it's there any simple way to create an array of years and months
dynamically (2 loops) for an associative array ?
I've tried this so far: (having an error on last line with the "=>" sign.

$table = array();
for($i=2005;$i<=date('y');$i++){
$table = array_push($i=>array());
for($d=1;$d<=12;$d++)
$table[$i] = array_push($table,$i=>0);
}

What I need is to create a table wich will then be written and readed
using $table[$year][$month] = $somevalue;

Thanks for helping.
Hi,

$table = array();
for($i=2005;$i<=date('y');$i++){
for($d=1;$d<=12;$d++)
$table[$i][$d] = "somevalue";
}

Something like that?

Regards,
Erwin Moller
Hi,

[Please don't toppost]

Can you give us an example of that targetarray?
eg:
[2005][1] = ...
[2005][2] = ...
???

It is not clear to me.
What is it you want as a result?
And where does the associative array come in?

Regards,
Erwin Moller
Jun 2 '08 #4

P: n/a
Erwin Moller schreef:
Bob Bedford schreef:
>Hi Erwin,

thanks for replying.

Reading is not a problem. I've a problem to "build" the associative
array.

"Erwin Moller"
<Si******************************************@spa myourself.coma
écrit dans le message de news:
48***********************@news.xs4all.nl...
>>Bob Bedford schreef:
Hi all,

it's there any simple way to create an array of years and months
dynamically (2 loops) for an associative array ?
I've tried this so far: (having an error on last line with the "=>"
sign.

$table = array();
for($i=2005;$i<=date('y');$i++){
$table = array_push($i=>array());
for($d=1;$d<=12;$d++)
$table[$i] = array_push($table,$i=>0);
}

What I need is to create a table wich will then be written and
readed using $table[$year][$month] = $somevalue;

Thanks for helping.
Hi,

$table = array();
for($i=2005;$i<=date('y');$i++){
for($d=1;$d<=12;$d++)
$table[$i][$d] = "somevalue";
}

Something like that?

Regards,
Erwin Moller

Hi,

[Please don't toppost]

Can you give us an example of that targetarray?
eg:
[2005][1] = ...
[2005][2] = ...
???

It is not clear to me.
What is it you want as a result?
[correction]
And where does the associative array come in?
I ment: Where does the associative array come into the picture?

Another thing: You said my samplecode 'reads' the array.
It does not, it creates it.

Regards,
Erwin Moller
Jun 2 '08 #5

P: n/a
>It is not clear to me.
>What is it you want as a result?

[correction]
>And where does the associative array come in?

I ment: Where does the associative array come into the picture?

Another thing: You said my samplecode 'reads' the array.
It does not, it creates it.

Regards,
Erwin Moller
sorry for top-posting..

here is the result...finally found on php.net site:

function array_push_associative(&$arr) {
$args = func_get_args();
foreach ($args as $arg) {
if (is_array($arg)) {
foreach ($arg as $key =$value) {
$arr[$key] = $value;
$ret++;
}
}else{
$arr[$arg] = "";
}
}
return $ret;
}

$table = array();
for($i=2005;$i<=date('Y');$i++){
$temp =
array($i=>array(1=>0,2=>0,3=>0,4=>0,5=>0,6=>0,7=>0 ,8=>0,9=>0,10=>0,11=>0,12=>0));
array_push_associative($table,$temp);
}
echo "<pre>";
print_r($table);
echo "</pre>";

Thanks for helping.

Bob
Jun 2 '08 #6

P: n/a
On May 14, 1:10 pm, "Bob Bedford" <b...@bedford.comwrote:
Hi all,

it's there any simple way to create an array of years and months dynamically
(2 loops) for an associative array ?
I've tried this so far: (having an error on last line with the "=>" sign.

$table = array();
for($i=2005;$i<=date('y');$i++){
$table = array_push($i=>array());
for($d=1;$d<=12;$d++)
$table[$i] = array_push($table,$i=>0);

}

What I need is to create a table wich will then be written and readed using
$table[$year][$month] = $somevalue;

Thanks for helping.
Just out of curiosity, why do you need to do this? PHP already has
some pretty comprehensive date and time functions.
Jun 2 '08 #7

P: n/a
Bob Bedford escribió:
here is the result...finally found on php.net site:

function array_push_associative(&$arr) {
$args = func_get_args();
foreach ($args as $arg) {
if (is_array($arg)) {
foreach ($arg as $key =$value) {
$arr[$key] = $value;
$ret++;
}
}else{
$arr[$arg] = "";
}
}
return $ret;
}

$table = array();
for($i=2005;$i<=date('Y');$i++){
$temp =
array($i=>array(1=>0,2=>0,3=>0,4=>0,5=>0,6=>0,7=>0 ,8=>0,9=>0,10=>0,11=>0,12=>0));
array_push_associative($table,$temp);
}
An array with numeric keys is *not* associative.

Erwin's code has a little typo —it should read date('Y') rather than
date('y')— but does exactly what you want. And it doesn't make you
wonder for a long while what the heck it does :)

I suggest you make the effort and try to learn how to read and write
arrays; blindly pasting codes you got somewhere and you don't understand
will only lead you to long term problems.
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jun 2 '08 #8

P: n/a

"Gordon" <go**********@ntlworld.coma écrit dans le message de news:
03**********************************...oglegroups.com...
On May 14, 1:10 pm, "Bob Bedford" <b...@bedford.comwrote:
>Hi all,

it's there any simple way to create an array of years and months
dynamically
(2 loops) for an associative array ?
I've tried this so far: (having an error on last line with the "=>" sign.

$table = array();
for($i=2005;$i<=date('y');$i++){
$table = array_push($i=>array());
for($d=1;$d<=12;$d++)
$table[$i] = array_push($table,$i=>0);

}

What I need is to create a table wich will then be written and readed
using
$table[$year][$month] = $somevalue;

Thanks for helping.

Just out of curiosity, why do you need to do this? PHP already has
some pretty comprehensive date and time functions.
Hi Gordon,

I've to create a table with a column for every month and a line for every
year then fill the values. Somes months on some year doesn't have values so
it was the easiest way to manage this.

Once I've the array, the table construction is done with a very few lines of
code.

Bob
Jun 2 '08 #9

P: n/a
Bob Bedford wrote:
>>It is not clear to me.
What is it you want as a result?
[correction]
>>And where does the associative array come in?
I ment: Where does the associative array come into the picture?

Another thing: You said my samplecode 'reads' the array.
It does not, it creates it.

Regards,
Erwin Moller

sorry for top-posting..

here is the result...finally found on php.net site:

function array_push_associative(&$arr) {
$args = func_get_args();
foreach ($args as $arg) {
if (is_array($arg)) {
foreach ($arg as $key =$value) {
$arr[$key] = $value;
$ret++;
}
}else{
$arr[$arg] = "";
}
}
return $ret;
}

$table = array();
for($i=2005;$i<=date('Y');$i++){
$temp =
array($i=>array(1=>0,2=>0,3=>0,4=>0,5=>0,6=>0,7=>0 ,8=>0,9=>0,10=>0,11=>0,12=>0));
array_push_associative($table,$temp);
}
echo "<pre>";
print_r($table);
echo "</pre>";

Thanks for helping.

Bob
Erwin's code is much cleaner. Just replace the "somevalue" with 0 and
it does exactly what you want.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #10

P: n/a
..oO(Erwin Moller)
>Bob Bedford schreef:
>Hi all,

it's there any simple way to create an array of years and months dynamically
(2 loops) for an associative array ?
I've tried this so far: (having an error on last line with the "=>" sign.

$table = array();
for($i=2005;$i<=date('y');$i++){
$table = array_push($i=>array());
for($d=1;$d<=12;$d++)
$table[$i] = array_push($table,$i=>0);
}

What I need is to create a table wich will then be written and readed using
$table[$year][$month] = $somevalue;

Thanks for helping.


Hi,

$table = array();
for($i=2005;$i<=date('y');$i++){
for($d=1;$d<=12;$d++)
$table[$i][$d] = "somevalue";
}

Something like that?
A bit late, but two short notes:

* It should be date('Y') to return a full four-digit year - 'y' only
returns two digits.

* For such things the array_fill() function can be quite handy:

$start = 2005;
$months = array_fill(1, 12, 0);
$table = array_fill($start, date('Y')-$start+1, $months);

Micha
Jun 2 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.