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[0]
= jan98,
$array[1] = feb98 ... $array[11] = 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[0]
> = jan98,
> $array[1] = feb98 ... $array[11] = 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  
 
 [ 3 post ] 

 Relevant Pages 

1. Playing with Forth and Need Help

2. newbie need help playing v86_call from plain pmode

3. Help playing .WAV files needed.

4. Help needed on LISP game playing

5. Enhanced PLAY.CMD with play list support

6. need help on pattern matching with array elements

7. Need help on arrays

8. Need Help Passing Safe Array to COM Object

9. Need help with creating array of subclasses

10. help Needed !!! EIP and Arrays

11. Sorting Array 3 Dimension..need help

12. Sorting array 3 dimension..need help

 

 
Powered by phpBB® Forum Software