Home > Blockchain >  PHP: array chunk where each chunk is not more than a fixed memory size
PHP: array chunk where each chunk is not more than a fixed memory size

Time:09-07

I have an array

$profileIds = [1, 2, 3, 4, 5, 6, 7, .............., 8000];

I want to get the minimum number of array chunks like [1,2,3], [4,5,6,7,8], [9, 10, 11, 12] where each chunk size is not more than fixed memory size i.e. 200KB. Thanks in advance.

I am getting the size of the array in bytes by doing:

$bytes = mb_strlen(serialize($array), '8bit');

CodePudding user response:

memory_get_usage() seems to do a good job with respect to getting current memory being used by the script. When you are creating a chunk, keep checking with the current memory used with previously recorded memory usage. Difference in both should give you the current memory being used by the chunk.

Snippet:

<?php

function getChunk($arr, &$idx, $chunkSizeInKB = 200){
  $chunkSize = $chunkSizeInKB * 1000; // in bytes
  
  $memOccupied = memory_get_usage();
  
  $res = [];
  
  for(; $idx < count($arr);   $idx){
    $res[] = $arr[ $idx ];
    if(memory_get_usage() - $memOccupied > $chunkSize){
      array_pop($res); // because inserting this one made it cross the chunk limit
      break;
    }
  }
  
  return $res;
}

$idx = 0;

$profileIds = range(1, 8000);

while($idx < count($profileIds)){
  echo count(getChunk($profileIds, $idx)),PHP_EOL; // or collect the chunk if you wish
}

Online Demo

CodePudding user response:

using mb_strlen(serialize($array), '8bit') is the correct way to calculate the array's size (but you will need memory_get_usage if you really want to know the total memory used in the script).

The solution will look like this:

<?php
$fixmem = 256;
for ($i = 0; $i < 5000; $i  ) {
    $arrayx[] = $i;
    $bytes = mb_strlen(serialize($arrayx), '8bit');
    if ($bytes > $fixmem) {
        array_pop($arrayx);
        $bytes2 = mb_strlen(serialize($arrayx), '8bit');
        echo "This array's chunk size is $bytes2 bytes.".PHP_EOL;
        print_r($arrayx);
        unset($arrayx);
    } else {
        //do nothing
    }
}
  • Related