拆分数组并添加到组

I have the following array which consists of simply integers (IDs) within 8 sub arrays. I have 8 groups, and I need the integers from array 1 to go into the first group, array 2 into the second and so on. I intent to use sql inserts to achieve this.

My data is as follows:

Array
(
    [0] => Array
        (
            [0] => 4012
            [1] => 3914
            [2] => 4054
            [3] => 3911
            [4] => 4010
            [5] => 3979
        )

    [1] => Array
        (
            [0] => 3916
            [1] => 3946
            [2] => 4059
            [3] => 3924
            [4] => 4018
            [5] => 3967
        )

    [2] => Array
        (
            [0] => 3983
            [1] => 4057
            [2] => 3980
            [3] => 3901
            [4] => 4045
            [5] => 3955
        )

    [3] => Array
        (
            [0] => 3908
            [1] => 3898
            [2] => 3939
            [3] => 4036
            [4] => 4079
            [5] => 3479
        )

    [4] => Array
        (
            [0] => 3995
            [1] => 3910
            [2] => 4047
            [3] => 3988
            [4] => 3965
            [5] => 4080
        )

    [5] => Array
        (
            [0] => 3925
            [1] => 3561
            [2] => 4000
            [3] => 4061
            [4] => 3950
            [5] => 4058
        )

    [6] => Array
        (
            [0] => 3989
            [1] => 3964
            [2] => 3904
            [3] => 4070
            [4] => 3954
            [5] => 3984
        )

    [7] => Array
        (
            [0] => 3985
            [1] => 4044
            [2] => 4062
            [3] => 4014
            [4] => 3899
        )

)

The groups I have are:

  • Group 01a
  • Group 01b
  • Group 02a
  • Group 02b
  • Group 03a
  • Group 03b
  • Group 04a
  • Group 04b

The php I am running works well in that it creates the name of the groups as above, however the foreach which handles the smallgroups array does not work. It duplicates the smallusers 8 times, when I want to have just the first sub array for processing in the loop, then then next.

$count = 0;
foreach ($biggroup as $key => $users) {
    $smallgroups = array_chunk($users, ceil(count($users) / 8));
    for ($i = 1; $i <= 4; $i++) {
    $groupnum = sprintf("%02d", $count * 4 + $i);
        foreach (range('a', 'b') as $letter) {
            $nameofgroup = 'Group ' . $groupnum . $letter;
            foreach ($smallgroups as $f => $smallusers) {
            }
        }
    }
}
$count++;

I expect the following:

Group 01A 4012, 3914, 4054, 911, 4010, 3979

Group 01B 3916, 3946, 4059, 3924, 4018, 3967

etc

array_walk() - Apply a user supplied function to every member of an array

You can use array_walk, Here $arr is the original array

$groups = ['01a','01b','02a','02b','03a','03b','04a','04b'];
$res = [];
array_walk($arr, function($v, $k) use ($groups, &$res){
   $res[$groups[$k]] = $v;
});
echo '<pre>';
print_r($res);

Output:-

 Array
(
[01a] => Array
    (
        [0] => 4012
        [1] => 3914
        [2] => 4054
        [3] => 3911
        [4] => 4010
        [5] => 3979
    )

[01b] => Array
    (
        [0] => 3916
        [1] => 3946
        [2] => 4059
        [3] => 3924
        [4] => 4018
        [5] => 3967
    )
    ........
    ........
    ........

array_combine() - Creates an array by using one array for keys and another for its values

I am using a few array values and keys for demonstration

$arr = Array
(
  0 => Array
    (
        0 => 4012,
        1 => 3914
    ),

  1 => Array
    (
        0 => 3916,
        1 => 3946
    ),

  2 => Array
    (
        0 => 3983,
        1 => 4057
    ),

  3 => Array
    (
        0 => 3908,
        1 => 3898
    ),

  4 => Array
    (
        0 => 3995,
        1 => 3910
    )

);
$groups = ['01a','01b','02a','02b','03a'];
$res = array_combine($groups, $arr);
echo '<pre>';
print_r($res);

A different approach is to generate the group name starting from the index of the biggroup array. Comments in the code:

$count = 0;
foreach ($biggroup as $key => $users) {   //

    // defines the number
    $groupnum = floor($key/2) + 1;

    // the letter is decided whether the $key is even (a) or odd (b)
    $letter = ($key % 2 == 0) ? 'a' : 'b';

    // hence:
    $nameofgroup = 'Group ' . $groupnum . $letter;

    // get the array of users
    $smallgroups = array_chunk($users, ceil(count($users) / 8));

    // loop through them
    foreach ($smallgroups as $smallusers) {
        foreach ($smallusers as $u) {
            // do what you need to do with each user
            echo "$nameofgroup $u
";
        }
    }
}
$count++;

You can see this in action (with a reduced array size) here: https://eval.in/1101429

Output:

Group 1a 4012
Group 1a 3914
Group 1a 4054
Group 1a 3911
Group 1a 4010
Group 1a 3979
Group 1b 3916
Group 1b 3946
Group 1b 4059
Group 1b 3924
Group 1b 4018
Group 1b 3967
Group 2a 3983
Group 2a 4057
Group 2a 3980
Group 2a 3901
Group 2a 4045
Group 2a 3955