合并同一数组内不同集合内的数组

问题描述 投票:0回答:1

我得到一个包含集合的数组,并且在这些集合中又存在数组。我想要做的是将集合合并为一个集合,其中包含多个集合的所有数组。

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 [▶]
    }
  ]
}
php arrays laravel collections
1个回答
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",
    ],
}
© www.soinside.com 2019 - 2024. All rights reserved.