如何将其转置为数组?

时间:2018-10-26 03:19:45

标签: laravel eloquent

我正在尝试将Collection转换为数组。我不确定执行此操作的方法是什么。我认为这是由于我缺乏熟练的操作员/命令的理解。我一直在尝试map,但没有取得任何进展。

数据

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 ▶}
  ]
}

我想将其转置为数组['HIGH'=> 2,'MEDIUM'=> 1,'LOW'=> 13 ...]

我尝试应用地图,但没有给我我想要的东西。 (尝试应用以下内容)

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

寻找有关了解这些地图运算符以及如何转置上方Collection结果的提示。任何帮助将是最欢迎的!

1 个答案:

答案 0 :(得分:0)

使用toArray函数将集合转换为数组

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

希望此链接对您有所帮助。

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

相关问题