p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: next ( ) and prev ( ) array functions - what is actually "next"?


Message #1 by sandmman@d... on Tue, 26 Feb 2002 19:26:43
All documentation over these two array functions, next() and prev() 

describe their function as "advancing" the internal php pointer to 

the "next" or "following" element in the array, for next() - or the 

reverse, i.e. to the "previous" for prev() function. But the documentation 

does not define "next" or "following" in an nonsequential array?

But what is actually the "next" or "following" element in an array like 

this one:



array_element[5]

array_elebent[10]

array_eleemnt[1]

array_element[6]



If the pointer starts at array_element[5], is the "next" or "following" 

element of the array, for purposes of the next() function, the next one 

physically, so the second element in this array, array_element[10]? 

OR 

Is the "next" element after array_element[5] for purposes of the next() 

function, the next-in-order by index value, so array_element[6], in this 

case?



Thanks.



sandmman

Message #2 by "Nikolai Devereaux" <yomama@u...> on Tue, 26 Feb 2002 16:43:25 -0800

It really wouldn't have been too hard for you to test this on your own,

would it?



It probably took longer for you to type the email than it would've to type

this in an editor and run it:



<?

   $array_element[5] = "five";

   $array_element[10] = "ten";

   $array_element[1] = "one";

   $array_element[6] = "six";



   reset($array_element);



   while($value = next($array_element))

   {

        echo $value;

   }

?>



Anyway, since I already typed it, I'll just tell you that running it gives

you this:

  tenonesix



which leads me to believe that the array is represented in the order you

added items to it, and not sorted by the integer keys.



One also should note that "five" was skipped even though we reset the array

pointer to the beginning.  current() would've returned "five", but next

advances the internal pointer BEFORE returning the value.





nik



> -----Original Message-----

> From: sandmman@d... [mailto:sandmman@d...]

> Sent: Tuesday, February 26, 2002 7:27 PM

> To: beginning php

> Subject: [beginning_php] next ( ) and prev ( ) array functions - what is

> actually "next"?

>

>

> All documentation over these two array functions, next() and prev()

> describe their function as "advancing" the internal php pointer to

> the "next" or "following" element in the array, for next() - or the

> reverse, i.e. to the "previous" for prev() function. But the

> documentation

> does not define "next" or "following" in an nonsequential array?

> But what is actually the "next" or "following" element in an array like

> this one:

>

> array_element[5]

> array_elebent[10]

> array_eleemnt[1]

> array_element[6]

>

> If the pointer starts at array_element[5], is the "next" or "following"

> element of the array, for purposes of the next() function, the next one

> physically, so the second element in this array, array_element[10]?

> OR

> Is the "next" element after array_element[5] for purposes of the next()

> function, the next-in-order by index value, so array_element[6], in this

> case?

>

> Thanks.

>

> sandmman

>




> $subst('Email.Unsub').




  Return to Index