调用未定义的函数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.

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

That's a helper method. try running

composer dumpautoload

if that doesn't solve can you try and see whether composer.json contains the file containing helper method autoloaded like below. helper file should contain the method array_build. name of the file might not be helpers.php

"files":["app/helpers.php"].

as @ishegg has mentioned it has been dropped. so if you want your code to work here is a way to do it.

create a file like this in project root or somewhere

helpers.php

<?php

if ( ! function_exists('array_build'))
{
    /**
     * Build a new array using a callback.
     *
     * @param  array     $array
     * @param  \Closure  $callback
     * @return array
     */
    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;
    }
}

and in composer.json autoload the file like this

"autoload": {
    "psr-4": {
        .....
    },
    "files": [
        "helpers.php"
    ]
},

run

composer dumpautoload