PHP code example of yannoff / collections

1. Go to this page and download the library: Download yannoff/collections 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/ */

    

yannoff / collections example snippets




// Using PHP native arrays, the classic way:
$countries = [ 'France', 'Italia' ];

// Push an element
array_push($countries, 'Switzerland');

// Append an element
$countries[] = 'Belgium';




use Yannoff\Component\Collections\Collection;

// Using collections:
$countries = new Collection([ 'France', 'Italia' ]);

// Push an element
$countries->push('Switzerland');

// Append an element: the same syntax as for arrays
$countries[] = 'Belgium';

// Exporting the collection back to a native PHP array:
$array = $countries->all();

// As for native arrays, collections can be traversed with a foreach:
foreach($countries as $key => $value) {
    // ...
}