PHP code example of dtkahl / php-array-tools

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

    

dtkahl / php-array-tools example snippets


$map = new Map([
    'first_name' => 'Jeffrey',
    'last_name' => 'Clarence',
    'age' =>  24
]);

$map->each(function ($key, $value) {
  // do something
});

$map->map(function ($key, $value) {
  return "Mapped " . $value;
});

$collection = new Collection([
    [
        'first_name' => 'Jeffrey',
        'last_name' => 'Clarence',
        'age' =>  24
    ],
    [
        'first_name' => 'Neil',
        'last_name' => 'Hiram',
        'age' =>  32
    ],
    [
        'first_name' => 'Derek',
        'last_name' => 'Deon',
        'age' =>  19
    ],
]);

$collection->each(function ($item, $key) {
  // do something
});

$collection->filter(function ($item, $key) {
  return $item['age'] > 10;
});

$collection->sort(function ($a, $b) {
  return $a['age'] > $b['age'];
});

$collection->map(function ($item) {
  return $item['first_name] . " " . $item['last_name];
});

while ($item = $collection->next()) {
  var_dump($item['age']);
  echo "<br>";
}

$collection->lists(['age']) // array (array('age'=>24'), array('age'=>32), array('age'=>19))

composer