How to make big array (26000 x 26000) using PHP?
Asked Answered
J

1

6

i try to make array with PHP. The dimension of the array is 26000 x 26000. Is it possible to make array that big? I already try to make an array with dimension 10000 x 10000 but the program keep telling me this:

Fatal error: Out of memory (allocated 1886388224) (tried to allocate 24 bytes) in C:\xampp\htdocs\matrix\index.php on line 24

i have 8GB RAM, i already set the memory_limit in php.ini with -1 (apache configuration). the code for build the array is this:

function zeros($rowCount, $colCount)
{
    $matrix = array();
    for ($rowIndx=0; $rowIndx<$rowCount; $rowIndx++)
    {
        $matrix[] = array();
        for($colIndx=0; $colIndx<$colCount; $colIndx++)
        {
            $matrix[$rowIndx][$colIndx]=0;
        }
        var_dump(memory_get_usage());
    }
    return $matrix;
}

$matrix = zeros(25000,25000);

I also already try using SplFixedArray, but the result is the same. Please help me, thanks! :)

Jowl answered 25/4, 2016 at 12:40 Comment(4)
I have to wonder what your use case is for such large arrays. Is it possible, yes, though I'd expect you'd need more memory before long, but is it necessary.Staphylo
If you're using straight numeric indexing, and you know the dimensions of your array in advance; why not consider SPLFixedArray insteadCharron
I tested from 100,100 to 600, 600: memoryUsage / arrayElements ≈ 200 Bytes so in your case it would need 25000 * 25000 * 200 Bytes ≈ 100GBCarlile
Essential reading if you're trying to work with large arrays in PHPCharron
B
-1

You can use this piece of code too

    $mon = range(1, 26000);
    for($i=0;$i<=25999;$i++){
        $mon[$i]    =   range(1, 26000);
    }
Bluet answered 15/9, 2017 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.