如何使用Arr :: only helper重命名数组的键?

我正在使用Arr::only通过键从数组中获取值,但是我需要重命名ShortDesc键以使用蛇形而不是驼峰式。 Arr::only有可能吗?还是需要使用array_map?

这是我出色的代码。

$myArray = Arr::only($targetarray,["title","shortDesc"]);
zjd1987zjd 回答:如何使用Arr :: only helper重命名数组的键?

查看代码:https://github.com/illuminate/support/blob/master/Arr.php#L367

否,仅使用Arr :: only函数无法实现所需的功能。 您将需要更多。

,

为此,您可以使用collections代替辅助方法:

$array = [
    'title'          => 'The title','shortDesc'      => 'The short description','someOtherValue' => 'foobar',];

$newArray = collect($array)
    ->only('title','shortDesc')
    ->keyBy(function ($item,$key) {
        return Str::snake($key);
    })->toArray();
本文链接:https://www.f2er.com/3096050.html

大家都在问