1

Note, array_chunk is not my solution (It seems to me).

I have an array of about 150.000 elements

Array
(
    [0] => Array
        (
            [name] => Danilo
            [phone] => 33568
        )

    [1] => Array
        (
            [name] => Alessandro
            [phone] => 392222
        )

    [2] => Array
        (
            [name] => Alex
            [phone] => 3922
        )

    [3] => Array
        (
            [name] => Capa
            [phone] => 392
        )

)

And so on. I would split this array in several arrays, of (for example) 3.000 elements every one.

I saw array_chunk, but it returns a single array with several subarray.

I need several subarray to store them in a database and elaborate in future.

I'm getting crazy to write a snippet starting from that $temp and divide it into smaller array.

$size_chunks = 1;

        $temp = array_chunk($recipients, $size_chunks);

        foreach ($temp as $key=>$value)
        {
            if ($key<$size_chunks)
            {
                $to_store[] = $temp[$key];  
            }
            //print_r($to_store);
            // pseudo sql
            // INSERT INTO table (sub_recipient) VALUES ($to_store);
            $to_store = array();

        }

So, every time that for loop end, reduce temp, store $to_store array and restart for others chunks.

Thank you very much.

PS in my example chunk==1 because starting array is small... ;)

With my example of chunk = 1, I need from starting array this 4 arrays:

Array
(
    [0] => Array
        (
            [name] => Danilo
            [phone] => 33568
        )
)

Array
    (
        [0] => Array
            (
                [name] => Alessandro
                [phone] => 39222
            )
    )

Array
        (
            [0] => Array
                (
                    [name] => Alex
                    [phone] => 39222
                )
        )

Array
        (
            [0] => Array
                (
                    [name] => Capa
                    [phone] => 392
                )
        )

Another explain

1 - With a starting array of 15.000 elements, and chunk of 3.000, I need in output (15.000 / 3.000) = 5 arrays. I will save them in database, so in DB I will have 5 rows (a row for every array).

2 - With a starting array of 4 elements, and chunk of 1, I need in output (4 / 1) = 4 arrays. I will save them in database, so in DB I will have 4 rows (a row for every array).

7
  • 1
    Can you include expected output? I find it difficult to understand why array_chunk() doesn't fit your scenario. Commented Apr 22, 2015 at 9:11
  • 1
    I saw array_chunk, but it returns a single array with several subarray. I'm not sure how that does not match your needs, you would need to store the chunks somewhere, right? Commented Apr 22, 2015 at 9:13
  • @sineverba can you enter expected output what you want do here. Commented Apr 22, 2015 at 9:22
  • 1
    And how would you address your arrays? You could give them all different names but storing them in an array would make them a lot easier to address and use... Commented Apr 22, 2015 at 9:27
  • 3
    And where would you store those 4 arrays? In 4 different variables? Why not, uhm, in an array instead? Commented Apr 22, 2015 at 9:27

3 Answers 3

3

array_chunks() already does what you want, you just have to save it:

$chunks = array_chunk($array, $size_chunks);

foreach ($chunks as $chunk) {
    // save $chunk to your database
}
Sign up to request clarification or add additional context in comments.

Comments

1

If anybody looking for divide an array into 2

$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel", "Opel2");

$count = count($cars);
$count = round($count/2);
echo "<pre>";
array_chunk($cars,$count);

Comments

0
$recipients = Array(
                    Array("fdbvfdb","dsacsdcds"),
                    Array("hrloo","dacdsc"),
                    Array("dcsdc","adcsd"),
                    Array("dcsdc","adcsd")
                );
$total = count($recipients);//count 150.000 elements
$i=1;
for($i=0;$i<$total;$i++){
$O = array_slice($recipients,$i,1);
print_r($O);
//Your insert/Save code
}

you can use this code there is uses Array_Slice

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.