Laravel Collection - Return 仅值,不需要键
Laravel Collection - Return value only, don't want key
我有一组Laravelcollection,但是我只需要数值,看下面的例子collection,我只需要A1
,1.00
,我不需要 header 1
,header 2
...
下面是collection.
{
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();
但是,我总是得到全套 collection,包括 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();
我有一组Laravelcollection,但是我只需要数值,看下面的例子collection,我只需要A1
,1.00
,我不需要 header 1
,header 2
...
下面是collection.
{
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();
但是,我总是得到全套 collection,包括 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();