将数组合并到同一数组中的不同集合中

时间:2020-08-06 09:43:09

标签: arrays laravel collections

我得到一个带有集合的数组,并且在这些集合内又有一个数组。我想做的是将集合合并为只有一个集合与多个集合的所有数组。

Collection {#1592 ▼
  #items: array:4 [▼
    0 => Collection {#1595 ▼
      #items: array:2 [▶]
    }
    1 => Collection {#1589 ▶}
    2 => Collection {#1585 ▼
      #items: array:2 [▶]
    }
    3 => Collection {#1579 ▼
      #items: array:2 [▶]
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

您可以使用集合的flatten()方法:

即)

$a = collect(['a', 'b', 'c']);
$d = collect(['d', 'e', 'f']);
$g = collect(['g', 'h', 'i']);

$c = collect([$a, $d, $g]);

$c->flatten();

将输出:

Illuminate\Support\Collection {#3124
    all: [
        "a",
        "b",
        "c",
        "d",
        "e",
        "f",
        "g",
        "h",
        "i",
    ],
}
相关问题