如何将转换后的数组提取到集合

时间:2019-11-08 12:08:23

标签: laravel eloquent

我将此数组转换为集合

array:1 [
"params" => array:3 [
"total_amount" => "2.37"
"user_id" => 3
"used_currency" => "YER"
]
]
$items = collect(request()->all());

当将total_amount值转换为集合时,如何采摘它。

dd($items->get('params.total_amount'));

2 个答案:

答案 0 :(得分:1)

只需访问集合中的数组键

dd($items->get('params')['total_amount']);

希望这会有所帮助

答案 1 :(得分:0)

您仅将外部数组转换为集合,而不是内部数组。

要将所有数组转换为集合,请在json_decode之后使用json_encode

$abc =  [
    "params" => [
        "total_amount" => "2.37",
        "user_id" => 3,
        "used_currency" => "YER"
    ]
];
$collect = collect(json_decode(json_encode($abc)));

return dd($collect->pluck('total_amount'));

这样,如果您的数据如下所示,则可以使用所有收集方法,例如所有“ total_amount”之和

$abc =  [
    "params1" => [
        "total_amount" => "2.37",
        "user_id" => 3,
        "used_currency" => "YER"
    ],
    "params2" => [
        "total_amount" => "2.37",
        "user_id" => 3,
        "used_currency" => "YER"
    ],
];
$collect = collect(json_decode(json_encode($abc)));
return dd($collect->pluck('total_amount')->sum());