newbtophp Posted February 18, 2010 Share Posted February 18, 2010 I need some help, in grouping numbers in lots of 15. Im trying to figure out a way to sort an array of numbers in numerical order, then create a new array which will consist of the numbers being in numerical order and have next to them their group number followed by their order number. This is what i expect it to be like: Input: array("26", "29", "27", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45"); Output: Array ( [26] => 1, 1 [27] => 1, 2 [29] => 1, 3 [30] => 1, 4 [31] => 1, 5 [32] => 1, 6 [33] => 1, 7 [34] => 1, 8 [35] => 1, 9 [36] => 1, 10 [37] => 1, 11 [38] => 1, 12 [39] => 1, 13 [40] => 1, 14 [41] => 1, 15 [42] => 2, 1 [43] => 2, 2 [44] => 2, 3 [45] => 2, 4 ) This is so far what I've come up with: <?php $ids = array("26", "29", "27", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45"); //sort them in numerical order... sort($ids, SORT_NUMERIC); print_r($ids); ?> All help is greatly appreciated, as I've tried myself and seems pretty complex :-\ Thanks. Link to comment https://forums.phpfreaks.com/topic/192473-arrange-and-group-numbers-in-an-array/ Share on other sites More sharing options...
Garethp Posted February 18, 2010 Share Posted February 18, 2010 //sort them in numerical order... sort($ids, SORT_NUMERIC); $i = 0; $x = 0; $New = array(); foreach($ids as $v) { $T = $i + 1; $New[$i][] = $T . ", " . $v; $x++; if($x ==15) { $i++; } } print_r($New); Woops, misread post. New reivion: $ids = array("26", "29", "27", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45"); //sort them in numerical order... sort($ids, SORT_NUMERIC); $i = 1; $x = 1; $New = array(); foreach($ids as $v) { $New[$v] = "$i, $x"; $x++; if($x == 15) { $x = 1; $i++; } } Link to comment https://forums.phpfreaks.com/topic/192473-arrange-and-group-numbers-in-an-array/#findComment-1014153 Share on other sites More sharing options...
teamatomic Posted February 18, 2010 Share Posted February 18, 2010 Counting starts at 0, when you initiate a var at 1 and and compare with == and you need a grouping of 15 you must go one higher, thus count to 16, to use the grouping number you must compare using a greaterThan. if($x == 15)//wrong if($x == 16)//right if($x > 15)//right HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/192473-arrange-and-group-numbers-in-an-array/#findComment-1014158 Share on other sites More sharing options...
yozyk Posted February 18, 2010 Share Posted February 18, 2010 sort($ids, SORT_NUMERIC); for($i = 0, $j = 1, $cnt = count($ids); $i < $cnt; $j = (++$i+1)%15) $newIds[$ids[$i]] = ceil(($i+1)/15).', '.($j?$j:15); echo '<pre>',print_r($newIds, 1),'</pre>'; Link to comment https://forums.phpfreaks.com/topic/192473-arrange-and-group-numbers-in-an-array/#findComment-1014176 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.