[TYPO3-mvc] Strange usage of array_fill_keys()

Jigal van Hemert jigal.van.hemert at typo3.org
Sat Apr 13 14:21:51 CEST 2013


Hi,

On 13-4-2013 13:15, Stefan Neufeind wrote:
> during review of a patch I stumbled across something I couldn't make
> sense of. And the comment seems to indicate somebody else had the same
> problem :-)
> in Typo3DbBackend.php, getRowsFromResult() is just:
>
[...]
>        // TODO Check if this is necessary, maybe the last line is enough
>        $arrayKeys = range(0, count($row));
>        array_fill_keys($arrayKeys, $row);
>        $rows[] = $row;

The first code line creates and array with the numbers 0 to number of 
fields in the db record. This is already odd because it contains one 
item more than the db record.

The second code line creates an array with the previous array as keys 
and the $row as the value for each item in the array.
So, if $row = array(
'uid' => 1,
'pid' => 2,
'crdate' => 13457694
);
Then we end up with an array(
   0 => array(
     'uid' => 1,
     'pid' => 2,
     'crdate' => 13457694
   ),
   1 => array(
[...]
   3 => array(
     'uid' => 1,
     'pid' => 2,
     'crdate' => 13457694
   ),
)
In the end nothing is done with this array (the return value of 
array_fill_keys() is simply ignored.

The last line of code adds $row to the array of $rows.

> In case it makes sense in some way (which I'm not really sure yet) the
> arrayKeys-array is one element too long. But practically that seems to
> be of no importance.

The two lines only burn some CPU cycles and waste a bit of memory for 
the PHP garbage cleaner to pick up.

Can you make an issue and push a patch to gerrit? This is a really 
simple way to gain (a tiny amount of) speed.

-- 
Jigal van Hemert
TYPO3 CMS Core Team member

TYPO3 .... inspiring people to share!
Get involved: typo3.org


More information about the TYPO3-project-typo3v4mvc mailing list