PHP code example of ivandelabeldad / collections

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

    

ivandelabeldad / collections example snippets


// CREATE THE LIST
$list = new ArrayList();

// ADD ELEMENTS TO THE LIST
$list->add("element");
$list->addAll(["element1", "element2"]);

// REMOVE ALL ELEMENTS
$list->clear();

// REMOVE AN ELEMENT BASED ON ITS INDEX
$list->remove(0);

// ADD ELEMENT AT SPECIFIED POSITION
$list->addAt(10, "element in position 10");

// GET CURRENT SIZE OF THE LIST
$list->size();

$list = new ArrayList([
    'first',
    'second',
    'third',
    'fourth',
]);

// EDIT EACH ELEMENT
$list->forEachDo(function (&$element) {
    $element = ucfirst($element);
});

// MAPPING
$mapped = $list->map(function ($element) {
    return "Mapped " . $element;
});

// FILTERING
$filtered = $list->filter(function ($element) {
   return strlen($element) > 5;
});