如何将数组键转换为int

问题描述 投票:0回答:2

嗨,我正在使用 laravel,我有一个数组 这个数组有一些键,其中一些是字符串,我想将它们更改为 int,并且所有键都将是 int

$nativeContent = NativeContent::find($id);
$nativeContentView = $nativeContent->countViewNative()
            ->orderBy('created_at', 'ASC')
            ->get()
            ->groupBy(function($date) {
               return Carbon::parse($date->created_at)->format('m'); // grouping by months
            });

我的数组是这样的

php laravel
2个回答
1
投票

试试这个:

->groupBy(function($date) {
            return intval(Carbon::parse($date->created_at)->format('m')); // grouping by months
        });

0
投票

如果您有收藏,可以轻松实现,方法是:

$newListWithIntKeys = $nativeContentView->values();

这将使用从 0 到 N 开始的常规连续整数,而不是您的命名键。

已在 Laravel 10 中验证

© www.soinside.com 2019 - 2024. All rights reserved.