Giter Club home page Giter Club logo

array-helper's Introduction

array-helper

Latest Version on Packagist Build Status StyleCi Status

โšก๏ธ 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

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:

$ composer require awssat/array-helper

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

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.

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

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

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

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

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.

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

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

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

$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!

array_map to loop through all items and change them.

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

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

array_walk can be used for looping without changing items

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

or if you want to change the values

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

array_filter is great for filtering an array

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

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

Tests

Simply use:

$ composer test

Credits

License

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

array-helper's People

Contributors

abdumu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.