drnxnbf199049 2019-04-29 18:07 采纳率: 100%
浏览 6

拆分数组并添加到组

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

  • 写回答

2条回答 默认 最新

  • dsds661730652211 2019-04-29 18:38
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。