-4

I have simply the following array_chunk which only splits my array of IDs into 17.

It will work as expected if there is another value, but 18 it is never happy with and returns 17 chunks only.

There is a total of 201 in my original array. I'm separating a large group of users into 18 smaller groups of user IDs.

Is there a workaround for this?

if (!empty($users)) {
   $eighteengroups = array_chunk($users, ceil(count($users) / 18));
}

which returns this..

Array
(
    [0] => Array
        (
            [0] => 2903
            [1] => 1686
            [2] => 2925
            [3] => 3012
            [4] => 2931
            [5] => 2898
            [6] => 2840
            [7] => 2945
            [8] => 2973
            [9] => 3791
            [10] => 2937
            [11] => 2856
        )

    [1] => Array
        (
            [0] => 2958
            [1] => 2962
            [2] => 2977
            [3] => 2860
            [4] => 2907
            [5] => 2472
            [6] => 3805
            [7] => 2919
            [8] => 2952
            [9] => 2901
            [10] => 2842
            [11] => 2847
        )

    [2] => Array
        (
            [0] => 3798
            [1] => 2989
            [2] => 2311
            [3] => 2889
            [4] => 2976
            [5] => 2220
            [6] => 2208
            [7] => 2918
            [8] => 2323
            [9] => 3795
            [10] => 2934
            [11] => 2330
        )

    [3] => Array
        (
            [0] => 3435
            [1] => 2911
            [2] => 3010
            [3] => 3799
            [4] => 2908
            [5] => 2928
            [6] => 3807
            [7] => 2915
            [8] => 3015
            [9] => 3005
            [10] => 2892
            [11] => 2862
        )

    [4] => Array
        (
            [0] => 2916
            [1] => 2987
            [2] => 2923
            [3] => 3004
            [4] => 2926
            [5] => 2848
            [6] => 2865
            [7] => 2906
            [8] => 2986
            [9] => 2959
            [10] => 2868
            [11] => 2341
        )

    [5] => Array
        (
            [0] => 2946
            [1] => 2853
            [2] => 2869
            [3] => 2875
            [4] => 2910
            [5] => 2978
            [6] => 2209
            [7] => 2917
            [8] => 2845
            [9] => 2988
            [10] => 2971
            [11] => 2936
        )

    [6] => Array
        (
            [0] => 3797
            [1] => 2333
            [2] => 2884
            [3] => 2504
            [4] => 3793
            [5] => 3000
            [6] => 2955
            [7] => 2262
            [8] => 2858
            [9] => 3794
            [10] => 2974
            [11] => 2899
        )

    [7] => Array
        (
            [0] => 2902
            [1] => 2920
            [2] => 2904
            [3] => 2922
            [4] => 2921
            [5] => 2994
            [6] => 3022
            [7] => 3792
            [8] => 2961
            [9] => 2993
            [10] => 2885
            [11] => 2933
        )

    [8] => Array
        (
            [0] => 2967
            [1] => 2877
            [2] => 2998
            [3] => 2854
            [4] => 2966
            [5] => 2956
            [6] => 3003
            [7] => 2980
            [8] => 2965
            [9] => 2992
            [10] => 3801
            [11] => 2309
        )

    [9] => Array
        (
            [0] => 2984
            [1] => 2843
            [2] => 2924
            [3] => 2975
            [4] => 3006
            [5] => 2878
            [6] => 2866
            [7] => 2997
            [8] => 2940
            [9] => 2949
            [10] => 2184
            [11] => 2944
        )

    [10] => Array
        (
            [0] => 2229
            [1] => 2852
            [2] => 2894
            [3] => 2241
            [4] => 2999
            [5] => 2873
            [6] => 2891
            [7] => 2315
            [8] => 2859
            [9] => 3796
            [10] => 2861
            [11] => 2914
        )

    [11] => Array
        (
            [0] => 2985
            [1] => 2896
            [2] => 3806
            [3] => 2841
            [4] => 2857
            [5] => 2957
            [6] => 2905
            [7] => 2969
            [8] => 2930
            [9] => 2968
            [10] => 2872
            [11] => 2890
        )

    [12] => Array
        (
            [0] => 2927
            [1] => 2880
            [2] => 2979
            [3] => 2935
            [4] => 3001
            [5] => 3002
            [6] => 2850
            [7] => 2883
            [8] => 3445
            [9] => 2954
            [10] => 2879
            [11] => 2900
        )

    [13] => Array
        (
            [0] => 2876
            [1] => 2888
            [2] => 2981
            [3] => 2317
            [4] => 2995
            [5] => 3007
            [6] => 2867
            [7] => 2912
            [8] => 2941
            [9] => 3803
            [10] => 2972
            [11] => 2948
        )

    [14] => Array
        (
            [0] => 2938
            [1] => 3014
            [2] => 2281
            [3] => 2874
            [4] => 2943
            [5] => 2863
            [6] => 3011
            [7] => 2950
            [8] => 2996
            [9] => 2991
            [10] => 2881
            [11] => 2963
        )

    [15] => Array
        (
            [0] => 2909
            [1] => 2897
            [2] => 2844
            [3] => 3008
            [4] => 3800
            [5] => 2183
            [6] => 3804
            [7] => 2929
            [8] => 2864
            [9] => 2964
            [10] => 2982
            [11] => 2932
        )

    [16] => Array
        (
            [0] => 2882
            [1] => 2887
            [2] => 2851
            [3] => 2960
            [4] => 2855
            [5] => 3802
            [6] => 2893
            [7] => 2990
            [8] => 2871
        )

)
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
user3189734
  • 665
  • 1
  • 9
  • 27
  • 188 id's array, chunked into 18 of each, last one will have 1 only. that's correct. so what's the issue here? – Alive to die - Anant Jun 19 '19 at 08:25
  • The second argument to `array_chunk` tells it how many items to put into each chunk. `ceil(201 / 18)` is 12, which corresponds to the result you get. What exactly did you expect…? – deceze Jun 19 '19 at 08:25
  • I have updated my code to a better example. It seemed to work with 188 users but not 201 as per updates. – user3189734 Jun 19 '19 at 08:27
  • 1
    If you want to split your array into 18 chunks, you need to put 11.1666… items into each chunk. Since that obviously won't work, you `ceil` the value, putting more items into one chunk, which obviously over large enough arrays results in fewer chunks. Not sure how you want to square this circle exactly. – deceze Jun 19 '19 at 08:28
  • This is not a duplicate as the other post referred to an array of 18 values. I'm looking for 18 chunks which is different. – user3189734 Jun 19 '19 at 09:08
  • Did you read the whole post? He says he want to split its 18 values into 12 segment. The size of segment changes, but the problem is the same.. – Ermenegildo Jun 19 '19 at 09:11
  • @user3189734 check this sample example link:- https://3v4l.org/8doeD . I hop this is what you want – Alive to die - Anant Jun 19 '19 at 10:54

1 Answers1

0

It does not return only 17, it returns 18. Arrays start with zero, not with one.

You can see it by looking at the first element, it has zero as a key.

Manuel Mannhardt
  • 2,191
  • 1
  • 17
  • 23