2017 © Pedro Peláez
 

library array-helper

A flexible & powerful array manipulation helper for PHP

image

awssat/array-helper

A flexible & powerful array manipulation helper for PHP

  • Saturday, March 17, 2018
  • by abdumu
  • Repository
  • 1 Watchers
  • 2 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

array-helper

Latest Version on Packagist Build Status StyleCi Status, (*1)

⚡️ A flexible, simple & yet powerful array manipulation helper for PHP. It gives you the magic of method chaining and it's easier and shorter to be included in views. It Supports most of PHP built-in array functions, (*2)

arr([' ', 'hi ', null, '  welcome'])->map('trim')->filter()->get()
>> ['hi', 'welcome']

Features

  • Support All PHP array functions.
  • Short methods names, no need to write "array_" like array_map can be only map(..)
  • Array items can be retrireve or updated as properties or keys [->key or [key] ]
  • Support powefull conditional methods. if, else, if{AnyMethod}, endif.
  • Support camelCase and snake-case methods name.
  • Useful new methods like equal, exists .. etc

Install/Use

You can install the package via composer locally in your project folder:, (*3)

$ composer require awssat/array-helper

After installing it, just start using the helper arr([...]) or ArrayHelper::make([...]), (*4)

Examples

Use any array function, no need for "array_", if you like it you can use it as in array_filter or arrayFilter .. all will work., (*5)

$x = arr(['', 'item', null, 'item2'])->filter()->get()
>> ['item', 'item2']

You can use conditions, if(function() {...}), if{AnyMethod}, else(), endif(), (*6)

$x = arr(['item', 'item2', null])
    ->ifContains(null)
        ->filter()
    ->endif()
    ->get()

you may also use useful method with if like ifEmpty, ifKeyExists or ifEqual etc., (*7)

get() will return all items, while get(index) return a item in the array. all() is alias for get(all, true) which will ignore conditions and force return of items anyway., (*8)

The example above can be shortened using all() like this:, (*9)

$x = arr(['item', 'item2', null])
    ->ifContains(null)
        ->filter()
    ->all()

You can use do(callback) to run a callback on the array., (*10)

$x = arr(['item ', 'item2'])
    ->do(function() {
        return $this->map('trim');
    })
    ->all()

PHP built-in array_map, array_walk, array_filter, and array_reduce are the best!, (*11)

array_map to loop through all items and change them., (*12)

$array->map(function($item) {
         return 'Hello: '. strip_tags($item) . ' !'; 
    });

or for simple functions use, $array->map('trim'), (*13)

array_walk can be used for looping without changing items, (*14)

$array->walk(function($item, $key) {
            print "$key: $item<br />\n";
    });

or if you want to change the values, (*15)

$array->walk(function(&$item, $key) {
            $item = $item * 2;
        });

array_filter is great for filtering an array, (*16)

$array->filter(function($item) {
            return $item > 5;
        });

or just $array->filter() to remove any value equal to FALSE., (*17)

Tests

Simply use:, (*18)

$ composer test

Credits

License

The MIT License (MIT). Please see License File for more information., (*19)

The Versions

17/03 2018

dev-master

9999999-dev https://github.com/awssat/array-helper

A flexible & powerful array manipulation helper for PHP

  Sources   Download

MIT

The Requires

  • php ~7.0

 

The Development Requires

by Awssat

helper array arrays manipulation pipechain

17/03 2018

1.0

1.0.0.0 https://github.com/awssat/array-helper

A flexible & powerful array manipulation helper for PHP

  Sources   Download

MIT

The Requires

  • php ~7.0

 

The Development Requires

by Awssat

helper array arrays manipulation pipechain

17/03 2018

dev-analysis-zRWMV9

dev-analysis-zRWMV9 https://github.com/awssat/array-helper

A flexible & powerful array manipulation helper for PHP

  Sources   Download

MIT

The Requires

  • php ~7.0

 

The Development Requires

by Awssat

helper array arrays manipulation pipechain