Created
November 3, 2010 06:52
-
-
Save mariovisic/660850 to your computer and use it in GitHub Desktop.
Basic Looping with 2+ incriment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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