0

I have an array of more than 10 items. What I need is to send a set of 10 values to a URL to return some JSON data. Save this JSON feed and send set of next 10 values (or the remaining ones) to that same URL and this continues until all the items are iterated.

More specifically how I can divide an array to subsets of 10 items each.

Array ( [0] => 249 [1] => 2827 [2] => 3228 [3] => 3327 [4] => 3567 [5] => 4259 [6] => 4547 [7] => 4607 [8] => 4660 [9] => 4677 [10] => 4783 [11] => 4807 [12] => 4934 [13] => 4944 [14] => 4977 [15] => 4990 [16] => 4992 [17] => 5021 [18] => 5056 [19] => 5061 ) 
2
  • 2
    Adding your code will help us in helping you Commented Feb 24, 2014 at 11:04
  • 3
    Try using array_slice: ro1.php.net/array_slice Commented Feb 24, 2014 at 11:05

2 Answers 2

2

See array_chunk function.

To example:

$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));

Result:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
        )

)
Sign up to request clarification or add additional context in comments.

Comments

0

Use array_chunk.

php.net/array_chunk

Something like:

$my_array = array('a', 'b', 'c', 'd', 'e');

// Divide into chunks of 2 (last chunk may contain less than 2)
$chunks = array_chunk($my_array, 2); 

// Loop through chunks (two items at a time)
while ($chunk = array_pop($chunks)) {
    // Do something with the next chunk
    var_dump($chunk);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.