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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

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

Post 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.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post 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.
Last edited by miro_igov on Fri Aug 17, 2007 1:14 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Alternately, you could use a flag to signal the beginning and prepend the data as needed.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

http_build_query()

Is that what your trying to accomplish?
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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.
Last edited by JellyFish on Fri Aug 17, 2007 1:40 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

next() wrote:next — Advance the internal array pointer of an array
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post 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.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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).
Last edited by JellyFish on Fri Aug 17, 2007 1:47 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post 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.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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.
Post Reply