调用未定义函数 App\Repositories\array_build()

Call to undefined function App\Repositories\array_build()

我的一位同事编写了为图表构建数组的代码:

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

我刚刚完成从 Laravel 5.2 到 5.3 的升级,现在出现以下异常:

Call to undefined function App\Repositories\array_build()

我不太确定他的代码是如何工作的(因此我没有找到 array_build 方法),因此无法让它恢复工作。

这是一个辅助方法。 尝试 运行宁

composer dumpautoload

如果那没有解决,您可以尝试查看 composer.json 是否包含包含辅助方法的文件,如下所示。帮助程序文件应包含方法 array_build。文件名可能不是 helpers.php

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

正如@ishegg 提到的那样,它已被删除。所以如果你想让你的代码在这里工作是一种方法。

在项目根目录或某处创建这样的文件

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;
    }
}

并在 composer.json 中像这样自动加载文件

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

运行

composer dumpautoload

array_build()was dropped in version 5.3,这就是为什么您在迁移后无法立即使用的原因。

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

可以得到函数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;
}

注意:来源非官方,official migration doc

中没有提到删除该功能