sineverba
sineverba

Reputation: 5172

PHP Split array in subarrays

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).

Upvotes: 1

Views: 3205

Answers (3)

MdFarzan
MdFarzan

Reputation: 390

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

Upvotes: 1

Ajeet Kumar
Ajeet Kumar

Reputation: 815

$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

Upvotes: 0

Daniel Antos
Daniel Antos

Reputation: 1216

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
}

Upvotes: 3

Related Questions