【问题标题】:How to transpose this into an array?如何将其转置为数组?
【发布时间】:2019-03-30 18:05:46
【问题描述】:

我正在尝试将 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 结果的提示。任何帮助将是最受欢迎的!

【问题讨论】:

标签: laravel eloquent


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多