【问题标题】:Laravel Collection - Return value only, don't want keyLaravel 集合 - 仅返回值,不需要键
【发布时间】:2021-02-17 10:41:43
【问题描述】:

我有一组 Laravel 集合,但我只需要值,看下面的示例集合,我只需要A11.00,不需要header 1header 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 1header 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->map(function ($inner) { return collect($inner)->values(); }) 这样的东西可能会起作用
  • @apokryfos 谢谢你,但这不是我想要的。查看我的更新。
  • 直接在$collection而不是子函数上运行
  • @apokryfos 是的,你是对的。我删除了 ($rows) 该行,现在它可以工作了!你的评论有帮助!谢谢
  • @apokryfos 抱歉想问一下,如果我想获得“标题 1”、“标题 2”,这可能吗? :)

标签: laravel laravel-5 laravel-collection


【解决方案1】:

考虑到您有以下数组

$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();

Live Demo

【讨论】:

  • 谢谢!这些是我需要的,你让它看起来很简单。
  • 如果我的回答对你有帮助,请尽可能投票。
  • 不知道为什么我不能投票,但我已经将其标记为答案。
【解决方案2】:

使用类似下面的东西。

$collect = $collection->map(function ($row) {
     return array_values($row);
});

dd($collect->values()->all());

【讨论】:

  • 抱歉想问一下,如果我想获得“header 1”、“header 2”,这可能吗? :)
  • 是的,如果你只想要键,那么你可以使用 array_keys($row)
【解决方案3】:

这样就可以了:)

$collect = $collection->map(function ($item) {
    return collect($item)->values();
});
return $collect->values()->all();

【讨论】:

    猜你喜欢
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 2018-12-10
    • 2019-04-03
    • 1970-01-01
    相关资源
    最近更新 更多