Search code examples
laravellaravel-5laravel-collection

Laravel Collection - Return value only, don't want key


I have a set of Laravel collection, but I only need the values, see the sample collection below, I only need A1, 1.00, and I don't need header 1, header 2...

Below is the 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"
},

Below is the code:

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

However, I always get the full set of collections including header 1, header 2.

Tried code below,

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

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

but the output is as below,

{
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"
]
},

Solution

  • Considering that you have a follwing array

    $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"
        ]
    ];
    

    If you need all the values

    $collectionOne = collect($arrayTbs)
            ->map(function($value,$key){
              return collect($value)->values();
            });
    

    if you need all those values on single array

    $collectiontwo = collect($arrayTbs)
            ->map(function($value,$key){
              return collect($value)->values();
            })->collapse();
    

    If you need all the keys

    $collectionThree = collect($arrayTbs)
            ->map(function($value,$key){
              return collect($value)->keys();
            });
    

    if you need all those keys on single array

    $collectionThree = collect($arrayTbs)
            ->map(function($value,$key){
              return collect($value)->keys();
            })->collapse();
    

    Live Demo