Help needed in playing with arrays
Author Message Help needed in playing with arrays

Hello -

I'm running into a problem in trying to get information within an
array printed out in the proper format.

I have an array that has the values (I think) of jan98 through dec98.
(I implicitly stuffed them in with a switch where I assigned \$array
= jan98,
\$array = feb98 ... \$array = dec98)

When I go to print this array out using a FOR loop:

for (\$n = 0; \$n < count(\$array); \$n++) {
\$line = each(\$array);
print ("\$line[key] = \$line[value]\n");

Quote:
}

Instead of getting:

0 = jan98
1 = feb98 ... 11 = dec98

I'm getting:

3 = apr98
7 = aug98
2 = feb98 ...

As if a sort has been done on \$array before it is being printed out.

I'm probably making a very stupid mistake, which would be par for the
course.

Any help would be appreciated. Please e-mail me at:

tony_perreault at hotmail dot com

Thanks.

Tony

Tue, 21 Jun 2005 14:04:04 GMT  Help needed in playing with arrays

Quote:

> When I go to print this array out using a FOR loop:

> for (\$n = 0; \$n < count(\$array); \$n++) {
>     \$line = each(\$array);
>     print ("\$line[key] = \$line[value]\n");
> }

I never used that type of construct. I prefer this:

foreach (\$array as \$key => \$value)
print ("\$key = \$value\n");

Quote:
> Instead of getting:

> 0 = jan98
> 1 = feb98 ... 11 = dec98

> I'm getting:

> 3 = apr98
> 7 = aug98
> 2 = feb98 ...

Have you tried print_r (\$array) to verify the contents of \$array?

Tue, 21 Jun 2005 16:25:17 GMT  Help needed in playing with arrays

Quote:

> Hello -

> I'm running into a problem in trying to get information within an
> array printed out in the proper format.

> I have an array that has the values (I think) of jan98 through dec98.
> (I implicitly stuffed them in with a switch where I assigned \$array
> = jan98,
> \$array = feb98 ... \$array = dec98)

> When I go to print this array out using a FOR loop:

> for (\$n = 0; \$n < count(\$array); \$n++) {
>     \$line = each(\$array);
>     print ("\$line[key] = \$line[value]\n");
> }

for (\$i=0;\$i<sizeof(\$array);\$i++){
print (\$i.'='.\$array[\$i]);

Quote:
}

Should work?

hth

H
-----------------------------------------
PHP,ASP,JSP Web Dev
http://www.howardstredwick.com

Tue, 21 Jun 2005 19:27:23 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages

Powered by phpBB® Forum Software