我正在使用Arr::only
通过键从数组中获取值,但是我需要重命名ShortDesc
键以使用蛇形而不是驼峰式。 Arr::only
有可能吗?还是需要使用array_map?
这是我出色的代码。
$myArray = Arr::only($targetarray,["title","shortDesc"]);
查看代码: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();