Asked  7 Months ago    Answers:  5   Viewed   43 times

I am wondering is there a size limit for array in php 5? I wrote a php script to change the last element of each line in a file. print out the content of a modified file, when I run the script on a small-sized file (i.e. each line comprises 4 values), it will work. When I run it on a larger file (like 60000 lines, each of which comprises 90 values), it does not change a bit of the original file, but the script did not throw any exception message during runtime. What is this problem?

 Answers

53

You could be running out of memory, as your array size is (in theory) only limited by the amount of memory allocated to the script. Put ini_set('memory_limit', '1024M'); in the beginning of your script to set the memory limit to 1 GB. You may need to increase this even higher for best results.

Wednesday, March 31, 2021
 
ChronoFish
answered 7 Months ago
39

When you set cell values individually, you have the option of setting the datatype explicitly, but when you use the fromArray() method, you don't have this option.

However, by default, PHP uses a default value binder to identify datatypes from the values passed, and set the cell datatype accordingly. This default behaviour is defined in a class /PHPExcel/Cell/DefaultValueBinder.php.

So you can create your own value binder, as described in the PHPExcel Documentation, that would set every value as a string datatype.

Something like:

class PHPExcel_Cell_MyColumnValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
{
    protected $stringColumns = [];

    public function __construct(array $stringColumnList = []) {
        // Accept a list of columns that will always be set as strings
        $this->stringColumns = $stringColumnList;
    }

    public function bindValue(PHPExcel_Cell $cell, $value = null)
    {
        // If the cell is one of our columns to set as a string...
        if (in_array($cell->getColumn(), $this->stringColumns)) {
            // ... then we cast it to a string and explicitly set it as a string
            $cell->setValueExplicit((string) $value, PHPExcel_Cell_DataType::TYPE_STRING);
            return true;
        }
        // Otherwise, use the default behaviour
        return parent::bindValue($cell, $value);
    }
}

// Instantiate our custom binder, with a list of columns, and tell PHPExcel to use it
PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_MyColumnValueBinder(['A', 'B', 'C', 'E', 'F']));

$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->fromArray($dataArray,null,"A2");
Friday, May 28, 2021
 
Wilk
answered 5 Months ago
13

There's a lot been written about PHPExcel and memory use, and I'm not going to repeat it all here.

Try reading some of the threads on the PHPExcel discussion board discussing the issue, such as this one; or previous answers here on SO such as this one or this one

Tuesday, July 6, 2021
 
Slinky
answered 4 Months ago
42

This seems to be as close to a work around as I can find. Do the inter-function calls from VBA

If you make something like this

Public Function funBA(n As Variant) As Variant
    funBA = funB(funA(n))
End Function

it seems to work up to n=2^24=2^8^3 (which doesn't look like any data type break point in VBA which is where the hang up is, but that's a pretty big array)

Saturday, August 14, 2021
 
d8aninja
answered 2 Months ago
71

No, I don't believe there's a limit to the depth of access (save for how much RAM you've got).

Each level adds at least one pointer indirection, though, so the speed of retrieving elements will suffer as you get deeper.

I'd be willing to bet dollars to donuts there's an easier way to store and manipulate the data you think you need this super-array for.

Wednesday, August 25, 2021
 
relyt
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :