What are DiscordPHP Collections?
masterCollections are containers for items that implement standard PHP interfaces, allowing them to behave like arrays. They are built around the concept of 'parts' and can be used for any data type.
Key behaviors include:
- Array Access: Use square bracket notation (e.g.,
$collec[123] = 'asdf';). - Iteration: Use
foreachloops to iterate over items. - Serialization: Supports
json_encode(), casting to(array), and casting to(string)(which performs a JSON encoding).
// square bracket index access
$collec[123] = 'asdf';
echo $collec[123]; // asdf
// foreach loops
foreach ($collec as $item) {
// ...
}
// json serialization
json_encode($collec);
// array serialization
$collecArray = (array) $collec;
// string serialization
$jsonCollec = (string) $collec; // same as json_encode($collec)