How to Split an Array into Parts Using PHP

How to Split an Array into Parts Using PHP

In this article we will see how to split an array into parts in order to process it without saturating the server.

In the PHP function library you have a very useful function that allows you to live an array in fragments (array_chunk ()).

In the example that we are going to illustrate we are going to use this function to live an array.

We will draw from a database of products containing Prestashop and we will keep in an array that we will call $ products. To split this array of products will use the following:

$ Parts = array_chunk ($ products, 5, true);

Array_chunk function contain 3 parameters:

  1. the array to be treated
  2. the size of each fragment
  3. And set the third parameter to true to preserve the array keys.

To scroll through each part will:

foreach ($ parts [$ step] as $ item) {
   //Batch process                           

Where the variable $ step represents the process step by parties.

Perform this operation you will be very useful when you have to handle a lot of information and the hosting server will not let you.

Lucid Kit

Related Posts
Leave a reply
Captcha Click on image to update the captcha .