Search code examples
phparrayslaravellaravel-5.3

Call to undefined function App\Repositories\array_build()


One of my colleagues wrote this code that builds an array for a chart:

$results = array_build(range($days - 1, 0), function ($k, $v) use ($dateFormat) {
            return [Carbon::today()->subDays($v)->format($dateFormat), [
                '0' => 0,
                '1' => 0
            ]];
        });

I just finished an upgrade from Laravel 5.2 to 5.3 and now get the following exception:

Call to undefined function App\Repositories\array_build()

I'm not exactly sure how his code works (hence I do not find the array_build method) and therefore cannot get it back working.


Solution

  • array_build() was dropped in version 5.3, which is why you can't use out of the box after your migration.

    array_build() helper is also removed from the framework as it’s no longer used anywhere in the core.

    You can get the function from the source:

    <?php
    function array_build($array, Closure $callback)
    {
        $results = array();
        foreach ($array as $key => $value)
        {
            list($innerKey, $innerValue) = call_user_func($callback, $key, $value);
            $results[$innerKey] = $innerValue;
        }
        return $results;
    }
    

    Note: the source is unofficial, there's no mention of dropping the function in the official migration doc