ninedoors Posted August 15, 2007 Share Posted August 15, 2007 I am trying to increment backwards through an array but having some problems. I will post the arrays that I am using to trouble shoot as these are not the arrays that I will be actually using. <? $array = array(2, 2, 5, 8, 21, 15, 16); $len = count($array); for($i = $len; $i <= $len; --$i) { $h = $array[$i]; echo $h; prev($array); } reset($array); ?> So what I what this to produce is an array that looks like (16, 15, 21, 8, 5, 2 , 2). Hope someone can help. Thanks Nick Quote Link to comment https://forums.phpfreaks.com/topic/64978-solved-incrementing-backwards-through-an-array/ Share on other sites More sharing options...
btherl Posted August 15, 2007 Share Posted August 15, 2007 prev() is a different way of doing things to $array[$i]. It has no effect here. Just do this: <?php $array = array(2, 2, 5, 8, 21, 15, 16); $len = count($array); for($i = $len - 1; $i >= 0; --$i) { $h = $array[$i]; echo $h . ", "; } echo "\n"; ?> Note that this only works when your array is indexed by consecutive integers. An alternative is to use array_reverse(), and foreach(). Quote Link to comment https://forums.phpfreaks.com/topic/64978-solved-incrementing-backwards-through-an-array/#findComment-324287 Share on other sites More sharing options...
Fadion Posted August 15, 2007 Share Posted August 15, 2007 As btherl said in the last line, u can use array_reverse(). To illustrate the idea: <?php $array = array_reverse(array(2, 2, 5, 8, 21, 15, 16)); foreach($array as $v){ echo $v; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64978-solved-incrementing-backwards-through-an-array/#findComment-324297 Share on other sites More sharing options...
dbo Posted August 15, 2007 Share Posted August 15, 2007 Personally I would suggest against reorganizing the original array and/or making a copy of it in reverse. I'd just do: $len = count($array); $i = ($len-1); while( $i >= 0 ) { echo $array[$i]; --$i; } For some reason I don't like the way a for loop looks when you're decrementing Quote Link to comment https://forums.phpfreaks.com/topic/64978-solved-incrementing-backwards-through-an-array/#findComment-324300 Share on other sites More sharing options...
Fadion Posted August 15, 2007 Share Posted August 15, 2007 Personally I would suggest against reorganizing the original array and/or making a copy of it in reverse. When dealing with huge arrays, reversing them and displaying may take a fraction more then counting and displaying, but otherwise i cant see why not using it. Quote Link to comment https://forums.phpfreaks.com/topic/64978-solved-incrementing-backwards-through-an-array/#findComment-324312 Share on other sites More sharing options...
dbo Posted August 15, 2007 Share Posted August 15, 2007 Yeah that's the only reason. Most cases it doesn't matter but rather than have to think about it I figure as a best practice it's just best to not do it. Not saying it's the 100% right way just my 2 cents. Quote Link to comment https://forums.phpfreaks.com/topic/64978-solved-incrementing-backwards-through-an-array/#findComment-324317 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.