I am trying to split an array into two parts.
$collection = collect( [1, 2, 15, 16] );
$groups = $collection->split( 2 );
The results are... mixed:
(2) [Array(2), {…}]
0: Array(2)
0: 1
1: 2
length: 2
__proto__: Array(0)
1:
2: 15
3: 16
__proto__: Object
length: 2
__proto__: Array(0)
I am not sure if it is a bug or I am missing something. If I split into more groups, the first one is always an array and others are objects. Why does it do that and how can I make the output type consistent?
P.S. A quick solution is to use array_chunk( $collection, 2 ) if the collections happens to be an array with numeric indexes as in the case of this example. According to the answer below, if the indexes are not numeric, then this problem should not occur.
__proto__property do there? I think this result is from the javascript consolejson_encodingarray with numeric keys starting not from0gives you an object.