Skip to content

Instantly share code, notes, and snippets.

@mariovisic
Created November 3, 2010 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariovisic/660850 to your computer and use it in GitHub Desktop.
Save mariovisic/660850 to your computer and use it in GitHub Desktop.
Basic Looping with 2+ incriment
<?php
# get the number of post elements
$count = count($_POST);
# get our array keys
$keys = array_keys($_POST);
# loop through them incrimenting by 2
# normally you would write $i++ at the end, this is shorthand for $i = $i + 1
# so naturally if you use + 2 we go up in steps of 2!
# you could use the shorthand $i += 2 instead! but the below works fine!
for($i = 0; $i < $count; $i = $i + 2)
{
# you can now access the two pair by using:
$first = $_POST[$keys[$i]];
$second = $_POST[$keys[$i + 1]];
# the second element may not always exist (if you have an odd number, so here you could check
# if it actually exists first!
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment