Page 1 of 1

Condition: If the array pointer is not at the end?

Posted: Fri Aug 17, 2007 1:06 pm
by JellyFish
How do I check if an array pointer is not at the last element in the array?

I'm using a common method for looping through an array:

Code: Select all

while (list($key, $value) = each($data))
  {
    $dataToPost .= "$key=$value";
    
    if (!attheend)
    {
      $dataToPost .= "&";
    }
  }
If I knew how to check if the array pointer isn't at the last element, then I could add an ampersand.

Posted: Fri Aug 17, 2007 1:12 pm
by miro_igov

Code: Select all

if(!next($array)) { 
 //we are at the end
}
else prev($array)

Edit: What i sometimes use is

Code: Select all

$dataToPost = rtrim($dataToPost,'&')
outside and after the loop.

Posted: Fri Aug 17, 2007 1:13 pm
by feyd
Alternately, you could use a flag to signal the beginning and prepend the data as needed.

Posted: Fri Aug 17, 2007 1:32 pm
by Zoxive
http_build_query()

Is that what your trying to accomplish?

Posted: Fri Aug 17, 2007 1:36 pm
by JellyFish
I like the rtrim idea the best, why didn't I think of that? *smack*

Anyways, for the case of learning: If I use the next function in a condition statement:

Code: Select all

if (!next($dataToPost))
{

}
Would that actually move the array pointer? So I would have to use prev() to move it back? What I'm asking kinda reminds me of:

Code: Select all

if (thart = thucker)
{

}
I can't remember if this was in PHP or Javascript but I know that if I use the '=' operator, rather then '==', in a conditional statement that it wouldn't just check for the condition, it would actually set thart to thucker.

Posted: Fri Aug 17, 2007 1:38 pm
by John Cartwright
next() wrote:next — Advance the internal array pointer of an array

Posted: Fri Aug 17, 2007 1:38 pm
by miro_igov
Yes it will move the pointer if there is another record and will return false if you are at the end. So in the else you should prev() and do the other tasks.

Posted: Fri Aug 17, 2007 1:43 pm
by JellyFish
Okay, I see.

But, does the php language have a way of preventing actual execution of the statement when it's in a conditional statement? Kind of a stupid question, because if there was one that you knew about you probably would have used it in the examples above.

EDIT:

Also, @jCart: I know that the function advances the array pointer. My question was, would it advance if it was in a condition. My question wasn't about the function so much, as it was more about the php language in general.

PS: Note the 'was' in "My question was, would it ad...", just in case someone is thinking that I'm still asking that question(which I'm not).

Posted: Fri Aug 17, 2007 1:45 pm
by John Cartwright
JellyFish wrote:Okay, I see.

But, does the php language have a way of preventing actual execution of the statement when it's in a conditional statement? Kind of a stupid question, because if there was one that you knew about you probably would have used it in the examples above.
That is an odd question. :? The answer is no.

Your best bet is probably following feyd's suggestion.

Posted: Fri Aug 17, 2007 1:47 pm
by miro_igov
The answer is no. Every condition is executed, this is why should not be used for($i=0;$i<count($array);$i++) /count() is executed on every iteration/, while($row = mysql_fetch_assoc(mysql_query('SELECT * FROM `table`')) /query is executed on every iteration and causes infinite loop/.

Example: if(isset($_GET['foo'])) executes the function isset() in the condition.

Posted: Fri Aug 17, 2007 1:52 pm
by JellyFish
miro_igov wrote:The answer is no. Every condition is executed, this is why should not be used for($i=0;$i<count($array);$i++) /count() is executed on every iteration/, while($row = mysql_fetch_assoc(mysql_query('SELECT * FROM `table`')) /query is executed on every iteration and causes infinite loop/.

Example: if(isset($_GET['foo'])) executes the function isset() in the condition.
Yeah, that makes sense. I don't think it would be a good idea not to have the statements executed, just that it was possible with an function or operator of some kind. Kinda like how void() prevents the statement from returning something.