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. 10 4348
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 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
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
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
>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
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.
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
--
"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
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
==================
..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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
12 posts
views
Thread by Treetop |
last post: by
|
12 posts
views
Thread by Susan Cranford |
last post: by
|
2 posts
views
Thread by Mary |
last post: by
|
7 posts
views
Thread by byoxemeek |
last post: by
|
19 posts
views
Thread by Ricardo Perez Lopez |
last post: by
|
6 posts
views
Thread by c19h28o2 |
last post: by
| | |
37 posts
views
Thread by mazwolfe |
last post: by
| | | | | | | | | | |