0

I am trying to transpose a Collection into an array. I'm not sure what's the method to do this. I think it's due to my lack of understanding in the eloquent operators/commands. I've been trying with map but had not made any headway.

Data

Collection {#911 ▼
  #items: array:4 [▼
    "HIGH" => Collection {#902 ▼
      #items: array:2 [▼
        0 => Finding {#680 ▶}
        1 => Finding {#681 ▶}
      ]
    }
    "MEDIUM" => Collection {#903 ▶}
    "LOW" => Collection {#904 ▶}
    "INFO" => Collection {#905 ▶}
  ]
}

I would like to transpose this into an array ['HIGH' => 2, 'MEDIUM' => 1, 'LOW' => 13 ...]

I tried to apply a map but it's not giving me what i want. (tried applying the below)

 ... ->map (function ($risk) { return $risk[0]; });

Looking for tips on learning about these map operators and also how to transpose the Collection result above. Any help will be the most welcome!

  • Possible duplicate of [laravel collection to array](https://stackoverflow.com/questions/35284974/laravel-collection-to-array) – reisdev Oct 26 '18 at 03:22
  • Hi @MatheusReis, you were right. I figured it out while you were submitting the comment. Thanks! – Floppy Fish Oct 26 '18 at 03:40

1 Answers1

0

Use toArray function to convert the collection to the array

$collection = collect(['name' => 'Desk', 'price' => 200]);
$collection->toArray();

Hope this link helps you.

https://laravel.com/docs/5.7/collections#method-toarray

Amol Rokade
  • 145
  • 1
  • 11