koolphp / koolreport

This is an Open Source PHP Reporting Framework which you can use to write perfect data reports or to construct awesome dashboards using PHP
https://www.koolreport.com/
MIT License
228 stars 65 forks source link

ArrayDataSource: Use current() instead of zero index of array #16

Open pawelkmpt opened 5 years ago

pawelkmpt commented 5 years ago

I encountered following issue PHP Notice: Undefined offset: 0 in .../koolphp/koolreport/src/datasources/ArrayDataSource.php on line 111 when used associative array with keys different than integers. As far as I can see, first item of array is used to detect columns type:

foreach($data[0] as $key=>$value)
{
    $metaData["columns"][$key]=array(
        "type"=>$this->guessType($value),
    );
}

I think you could use current($data) function instead of $data[0] so you get first element regardless the key name.

lkraav commented 5 years ago

I agree, this makes sense, since doesnt seem to have any side-effects:

The current() function simply returns the value of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way.

@pawelkmpt what's the secific use case for us? Why are non-integer keys better?

koolphp commented 5 years ago

I see, may be you following command to convert the array to using numeric keys before inserting to ArrayDataSource

$data = array_values($data);