Jump to content

Recommended Posts

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

//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++;
}
}

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.