问题描述
我有一组Laravel集合,但是我只需要值,看下面的示例集合,我只需要A1
、1.00
,我不需要header 1
,header 2
...
以下是合集。
{
header 1: "A1",header 2: "1.00",header 3: "2020-04-15",header 4: "D1 %",header 5: "1",header 6: "F1",header 7: "G1",header 8: "H1",header 9: "I1",header 10: "J1",header 11: "K1",header 12: "L1",header 13: "M1"
},{
header 1: "A2",header 2: "2.00",header 3: "2020-04-16",header 4: "D2",header 5: "2",header 6: "F2",header 7: "G2",header 8: "H2",header 9: "I2",header 10: "J2",header 11: "K2",header 12: "L2",header 13: "M2"
},
代码如下:
$collect = $collection->map(function ($rows) {
return collect($rows)->map(function ($item,$key) {
return $item;
});
});
return $collect->values()->all();
但是,我总是得到完整的集合,包括header 1
、header 2
。
试过下面的代码,
$collect = $collection->map(function ($rows) {
return collect($rows)->map(function ($item) {
return collect($item)->values();
});
});
return $collect->values()->all();
但输出如下,
{
header 1: [
"A1"
],header 2: [
"1.00"
],header 3: [
"2020-04-15"
],header 4: [
"D1 %"
],header 5: [
"1"
],header 6: [
"F1"
],header 7: [
"G1"
],header 8: [
"H1"
],header 9: [
"I1"
],header 10: [
"J1"
],header 11: [
"K1"
],header 12: [
"L1"
],header 13: [
"M1"
]
},
解决方法
这样就行了:)
$collect = $collection->map(function ($item) {
return collect($item)->values();
});
return $collect->values()->all();
,
使用如下所示的内容。
$collect = $collection->map(function ($row) {
return array_values($row);
});
dd($collect->values()->all());
,
考虑到你有一个以下数组
$arrayTbs = [
[
'header 1' => "A1",'header 2' => "1.00",'header 3' => "2020-04-15",'header 4' => "D1 %",'header 5' => "1",'header 6' => "F1",'header 7' => "G1",'header 8' => "H1",'header 9' => "I1",'header 10' => "J1",'header 11' => "K1",'header 12' => "L1",'header 13' => "M1"
],[
'header 1' => "A2",'header 2' => "2.00",'header 3' => "2020-04-16",'header 4' => "D2",'header 5' => "2",'header 6' => "F2",'header 7' => "G2",'header 8' => "H2",'header 9' => "I2",'header 10' => "J2",'header 11' => "K2",'header 12' => "L2",'header 13' => "M2"
]
];
如果你需要所有的值
$collectionOne = collect($arrayTbs)
->map(function($value,$key){
return collect($value)->values();
});
如果您需要单个数组上的所有这些值
$collectiontwo = collect($arrayTbs)
->map(function($value,$key){
return collect($value)->values();
})->collapse();
如果你需要所有的钥匙
$collectionThree = collect($arrayTbs)
->map(function($value,$key){
return collect($value)->keys();
});
如果您需要单个数组上的所有这些键
$collectionThree = collect($arrayTbs)
->map(function($value,$key){
return collect($value)->keys();
})->collapse();