PHP code example of originphp / collection
1. Go to this page and download the library: Download originphp/collection library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
originphp / collection example snippets
use Origin \Collection \Collection ;
$collection = new Collection($array);
$collection = collection($array);
$collection = collection($books);
$authors = $collection->extract('authors.name' );
$list = $authors->toList();
$collection = collection($books);
$books = $collection->extract(function ($book) {
return $book->name . ' written by ' . $book->author->name;
});
$list = $books->toList();
$collection = new Collection($books);
$collection->each(function ($value, $key) {
echo "{$key} - {$value}" ;
});
$collection = new Collection([
'a' =>1 ,'b' =>2 ,'c' =>3
]);
$plusOneCollection = $collection->map(function ($value, $key) {
return $value + 1 ;
});
$collection = new Collection($results);
$combined = $collection->combine('id' , 'name' );
$array = $combined->toArray();
$collection = new Collection($results);
$result => $collection->combine('id' , 'name' ,'profile' );
$array = $result->toArray();
$collection = new Collection([1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ]);
$chunks = $collection->chunk(5 );
$array = $chunks->toArray();
$collection = new Collection($books);
$inStock = $collection->filter(function ($book) {
return $book->in_stock === true ;
});
$collection = new Collection($books);
$notInStock = $collection->reject(function ($book) {
return $book->in_stock === true ;
});
$collection = new Collection($books);
$allBooksInStock = $collection->every(function ($book) {
return $book->in_stock > 0 ;
});
if ($allBooksInStock){
...
}
$collection = new Collection($books);
$anyThingInStock = $collection->some(function ($book) {
return $book->in_stock > 0 ;
});
if ($anyThingInStock){
...
}
$collection = new Collection($books);
$sortedCollection = $collection->sortBy('authors.name' );
$collection = new Collection($books);
$sortedCollection = $collection->sortBy(function ($book) {
return $book->author->name . '-' . $book->name;
});
$collection = new Collection($authors);
$author = $collection->min('rating' );
$collection = new Collection($books);
$author = $collection->min(function ($book) {
return $book->author->score;
});
$collection = new Collection($books);
$author = $collection->max('authors.rating' );
$collection = new Collection($books);
$author = $collection->max(function ($book) {
return $book->author->score;
});
$collection = new Collection($books);
$inStock = $collection->sumOf('in_stock' );
$collection = new Collection($books);
$points = $collection->sumOf(function ($book) {
return $book->author->rating;
});
$collection = new Collection($books);
$avgRating = $collection->avg('authors.rating' );
$collection = new Collection($books);
$avgRating = $collection->avg(function ($book) {
return $book->author->rating;
});
$collection = new Collection($books);
$median = $collection->median('authors.rating' );
$collection = new Collection($books);
$median = $collection->median(function ($book) {
return $book->author->rating;
});
$collection = new Collection($books);
$count = $collection->count();
$collection = new Collection($books);
$counts = $collection->countBy('authors.type' );
$collection = new Collection($books);
$counts = $collection->countBy(function ($book) {
return $book->id % 2 == 0 ? 'even' : 'odd' ;
});
$collection = new Collection($books);
$grouped = $collection->groupBy('authors.type' );
$array = $grouped->toArray();
$collection = new Collection($books);
$grouped = $collection->groupBy(function ($book) {
return $book->id % 2 == 0 ? 'even' : 'odd' ;
});
$array = $grouped->toArray();
$collection = new Collection($books);
$newCollection = $collection->insert('authors.registered' ,true );
$books = $newCollection->toArray();
$books = collection($books)->insert('authors.registered' ,true )->toArray();
$collection = new Collection($books);
$firstLot = $collection->take(10 );
$secondLot = $collection->take(10 ,11 );