PHP code example of fusion-php / collection
1. Go to this page and download the library: Download fusion-php/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');
/* Start to develop here. Best regards https://php-download.com/ */
fusion-php / collection example snippets
namespace App;
use Fusion\Collection\CollectionFactory;
.. or ...
$items = ['foo', 'bar', 'baz'];
$collection = CollectionFactory::newCollection($items);
var_dump($collection->count()); //int (3)
$size = count($collection); //same as $size = $collection->count();
$collection->add(42);
$collection = CollectionFactory::newCollection(['foo', 'bar', 'baz']);
var_dump($collection->find(1)); //string 'bar'
$collection = CollectionFactory::newCollection(['foo', 'bar', 'baz']);
$collection->replace(1, 'bam'); //Collection is now ['foo', 'bam', 'baz']
$collection = CollectionFactory::newCollection(['foo', 'bar', 'baz']);
$collection->remove('foo'); //count() == 2
$collection = CollectionFactory::newCollection(['foo', 'bar', 'baz']);
$collection->removeAt(1); //Collection is now [0 => 'foo', 1 => 'baz');
$collection = CollectionFactory::newCollection(['foo', 'bar', 'baz']);
$collection->clear();
var_dump($collection->count()); //int (0)
$collection = CollectionFactory::newCollection(['foo', 'bar', 'baz']);
for ($i = 0; $i < count($collection); $i++)
{
//... do something with $collection[$i];
}
// ... or ...
foreach ($collection as $key => $value)
{
//... do something with $key and/or $value
}
$collection = CollectionFactory::newCollection(['foo', 'bar', 'baz']);
var_dump($collection[2]); //string 'baz'
$collection[2] = 'qux'; //same as calling $collection->replace(2, 'qux');
unset($collection[3]); //same as calling $collection->removeAt(3);
namespace App;
use Fusion\Collection\CollectionFactory;
.. or ...
$items = ['foo' => 'bar', 'baz' => 'bam'];
$dictionary = CollectionFactory::newDictionary($items);
var_dump($dictionary->count()); //int (2)
$size = count($dictionary); //same as $size = $dictionary->count();
$dictionary->add('foo', 'bar');
$dictionary->replace('foo', 'bam');
$dictionary = CollectionFactory::newDictionary(['foo1' => 'bar', 'foo2' => 'bam']);
for ($i = 1; $i <= count($dictionary); $i++)
{
// ... do something with $dictionary['foo' . $i];
}
// ... or ...
foreach ($dictionary as $key => $value)
{
// ... do something with $key and/or $value
}
$dictionary = CollectionFactory::newDictionary(['foo' => 'bar', 'baz' => 'bam']);
var_dump($dictionary['baz']); //string 'bam'
unset($dictionary['foo']); // same as calling $dictionary->removeAt('foo');
$dictionary['foo'] = 'bar';
// same as calling $dictionary->add('foo', 'bar'); or $dictionary->replace('foo', 'bar');
namespace App;
use Fusion\Collection\CollectionFactory;
dCollection(Apple::class);
//Only instances of Apple are allowed
$apples->add(new Apple())
->add(new Apple())
->add(new Apple());
var_dump(count($apples)); //int (3)
// ... or ...
$setOfApples = [new Apple(), new Apple(), new Apple()];
$apples = CollectionFactory::newTypedCollection(Apple::class, $setOfApples);
var_dump(count($apples)); //int (3)
namespace App;
use Fusion\Collection\CollectionFactory;
ppleInterface { /* ... */ }
class GrannySmith implements AppleInterface { /* ... */ }
class Gala implements AppleInterface { /* ... */ }
$appleBasket = CollectionFactory::newTypedDictionary(AppleInterface::class);
$appleBasket->add('redDelicious', new RedDelicious())
->add('grannySmith', new GrannySmith())
->add('gala', new Gala());
namespace App;
use Fusion\Collection\Collection;
use Fusion\Collection\CollectionValidator;
z']);
var_dump(count($collection)); //int (3)