A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearHow to skip the 1st key in an array loop? - page 1
User InfoPosts
How to skip the 1st key in an array loop?#1
I have the following code:

if ($_POST['submit'] == "Next") {
foreach($_POST['info'] as $key => $value) {
echo $value;
}
}


How do I get the foreach function to start from the 2nd key in the array? Thanx.

posted date: 2008-12-16 06:52:00


Re: How to skip the 1st key in an array loop?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-16 06:52:01


Re: How to skip the 1st key in an array loop?#3
in loop:if ($key == 0) //or whatever continue;

posted date: 2008-12-16 06:54:00


Re: How to skip the 1st key in an array loop?#4
foreach(array_slice($_POST['info'], 1) as $key=>$value) { echo $value;}Alternatively if you don't want to copy the array you could just do:$isFirst = true;foreach($_POST['info'] as $key=>$value) { if ($isFirst) { $isFirst = false; continue; } echo $value;}

posted date: 2008-12-16 06:55:00


Re: How to skip the 1st key in an array loop?#5
For reasonably small arrays, use array_slice to create a second one:foreach(array_slice($_POST['info'],1) as $key=>$value){ echo $value;}

posted date: 2008-12-16 06:56:00


Re: How to skip the 1st key in an array loop?#6
I tend to advise against testing for 'special cases' in a loop, if they are known upfront (i.e. the first entry) -> -1

posted date: 2008-12-16 06:58:00


Re: How to skip the 1st key in an array loop?#7
yeah, removed. thanks

posted date: 2008-12-16 06:59:00


Re: How to skip the 1st key in an array loop?#8
This will skip the first 2 keys, but fix that small mistake and it's a good method

posted date: 2008-12-16 07:00:00


Re: How to skip the 1st key in an array loop?#9
Damn off-by-one errors :-)

posted date: 2008-12-16 07:03:00


Re: How to skip the 1st key in an array loop?#10
I like the isFirst solution better. Its in your face logic, the array slice version could easily be missed when scanning unfamiliar code.

posted date: 2008-12-16 07:20:00


Re: How to skip the 1st key in an array loop?#11
foreach($_POST['info'] as $key=>$value) { if ($key == 0) { //or what ever the first key you're using is continue; } else { echo $value; }}

posted date: 2008-12-16 09:59:00


Re: How to skip the 1st key in an array loop?#12
If you were working with a normal array, I'd say to use something like foreach (array_slice($ome_array, 1) as $k => $v {...but, since you're looking at a user request, you don't have any real guarantees on the order in which the arguments might be returned - some browser/proxy might change its behavior or you might simply decide to modify your form in the future. Either way, it's in your best interest to ignore the ordering of the array and treat POST values as an unordered hash map, leaving you with two options :copy the array and unset the key you want to ignoreloop through the whole array and continue when seeing the key you wish to ignore

posted date: 2008-12-16 10:09:00


select page: « 1 2...»
Copyright ©2008-2017 www.momige.com, all rights reserved.