newbtophp Posted May 2, 2010 Share Posted May 2, 2010 I have the following code, which arranges an array of numbers in numerical order, and outputs another array. How do i turn for every 10 $i's $i = 1, so say if $i = 20, $i would be 2, if $i was 6 $i would be 1, if $i was 30, $i would be 3, if $i was 1 it would remain 1.... <?php $ids = array('90', '96'); //sort them in numerical order... sort($ids, SORT_NUMERIC); $i = 0; $x = 0; $New = array(); foreach($ids as $v) { $i++; $x++; if($i < 10){ $i = 1; } $New[$v] = "&pg=$i#$x"; } ?> Sorry for bad description, sort of lost myself on how to describe it lol Link to comment https://forums.phpfreaks.com/topic/200466-if-i-10-i-10/ Share on other sites More sharing options...
newbtophp Posted May 2, 2010 Author Share Posted May 2, 2010 Heres some code to explain what i mean: <?php $i = 11; if($i <= 10){ $i = 1; } elseif($i >= 10 && $i <= 20){ $i = 2; } elseif($i >= 20 && $i <= 30){ $i = 3; } //etc. //would output 2 echo $i; ?> Link to comment https://forums.phpfreaks.com/topic/200466-if-i-10-i-10/#findComment-1051983 Share on other sites More sharing options...
Mchl Posted May 2, 2010 Share Posted May 2, 2010 $i = ceil($i/10); Link to comment https://forums.phpfreaks.com/topic/200466-if-i-10-i-10/#findComment-1051985 Share on other sites More sharing options...
Zane Posted May 2, 2010 Share Posted May 2, 2010 Don't overwrite $i.. create another variable for chrissakes. if($i >= 20) $x = 2; Link to comment https://forums.phpfreaks.com/topic/200466-if-i-10-i-10/#findComment-1051987 Share on other sites More sharing options...
newbtophp Posted May 2, 2010 Author Share Posted May 2, 2010 $i = ceil($i/10); Thanks that worked, xD, *feels dumb* Link to comment https://forums.phpfreaks.com/topic/200466-if-i-10-i-10/#findComment-1051988 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.