我正在使用Arr::only
通过键从数组中获取值,但是我需要重命名ShortDesc
键以使用蛇形而不是驼峰式。 Arr::only
是否可以使用?还是需要使用array_map?
这是我出色的代码。
$myArray = Arr::only($targetArray, ["title", "shortDesc"]);
您可以为此使用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://github.com/illuminate/support/blob/master/Arr.php#L367
不,仅使用Arr :: only函数无法实现您所需要的。您将需要更多。