Skip to content

Instantly share code, notes, and snippets.

@ryanscherler
Last active December 15, 2018 20:41
Show Gist options
  • Save ryanscherler/f7e76fa5d8e84a51068eb97c754acbff to your computer and use it in GitHub Desktop.
Save ryanscherler/f7e76fa5d8e84a51068eb97c754acbff to your computer and use it in GitHub Desktop.
Extends Laravel collections to convert array items to a collection recursively.
<?php
namespace App\Support\Collection;
use \Illuminate\Support\Collection;
class CustomCollection extends Collection {
/**
* Converts array items to a collections recursively.
* Usage: $collection = Collection::make($array)->collectArrayItems();
*
* @return mixed
*/
public function collectArrayItems()
{
$this->each(function($item, $key) {
if (is_countable($item)) {
$collection = self::make($item)->collectArrayItems();
$this->put($key, $collection);
}
});
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment