A5sh.com - 全球挚爱极客社区

标题: laravel集合函数—each() [打印本页]

作者: codefan    时间: 2024-1-15 13:28
标题: laravel集合函数—each()
each() 用于迭代集合中的每个元素



  1. $collection = collect(['one', 'two', 'three', 'four']);

  2. $data = [];
  3. $collection->each(function($item, $key) use(&$data) {
  4.     // 满足条件则 停止迭代
  5.     if($item == 'three' || $key == 2) {
  6.         return false;
  7.     }
  8.     $data[] = $item;
  9. });   
  10. return $data; // ["one","two"]
复制代码







欢迎光临 A5sh.com - 全球挚爱极客社区 (https://www.a5sh.com/) Powered by Discuz! X3.4