PHP code example of transprime-research / arrayed

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

    

transprime-research / arrayed example snippets


arrayed(1, 2, 'ninja')
    ->filter(fn($val) => is_int($val)) // [1,2]
    ->map(fn($val) => $val + 1) // [2, 3]
    ->flip() // [0, 1]
    ->values() // [0, 1]
    ->sum(); // 1

$result = array_filter([1, 2, 'ninja'], fn($val) => is_int($val));
$result = array_map(fn($val) => $val + 1, $result);
$result = array_flip($result);
$result = array_values($result);
$result = array_sum($result);

use Transprime\Arrayed\Arrayed;

// Nifty
arrayed(1, 2)->count();

// Easier
Arrayed::on(1, 2)->count();

// Normal with (new instance)
(new Arrayed(1,2))->count();

//Non associative
arrayed(1, 2);

//OR
arrayed([1, 2]);

// For associative array, only this way
arrayed(['a' => 1, 'b' => 2]); 

arrayed(1,2)->collect(); // instance of Illuminate/Support/Collection
arrayed(1,2)->collect(3, 4); //merged with first one to give [1, 2, 3, 4] 

collect(arrayed(1, 2, 3, 4));

// Or
new Collection(arrayed(1, 2, 3, 4));

// Or
Collection::make(arrayed(1, 2, 3, 4));

response()->json(arrayed(1, 2, 3)->flip());

arrayed(1, 2, 3)
    ->tap(function ($arrd) {
        logger('Array has '.$arrd->count());
    });

arrayed(['a' => 1, 'b' => 2])
    ->values() // returns array, we can chain
    ->sum(); // returns an integer, we cannot chain

arrayed(['a' => 'name', 'b' => 'age'])
    ->values()
    ->result(fn($val) => implode(',', $val)); //'name,age'

//Or

arrayed(['a' => 'name', 'b' => 'age'])
    ->values()(fn($val) => implode(',', $val)); //'name,age'

arrayed([1, 2])->raw(); //[1,2]

arrayed(['a' => 'www', 'b' => 'dot', 'c' => 'www'])
    ->pipe('array_unique') // data is piped forward to `array_unique`
    ->flip()
    ->values()(); //['a', 'b']

// ->combine() method is not yet implemented

arrayed(['a', 'b'])
    ->combine(['name', 'data'])
    ->result(); //['a' => 'name', 'b' => 'data']

use Illuminate\Support\Collection;

arrayed(1,2,3)->collectPipe(function (Collection $collected) {
    return $collected->take(2)->all();
})->keys();

static Arrayed::on(...$values): ArrayedInterface; //new instance of Arrayed

Arrayed::map($callback): ArrayedInterface;

Arrayed::filter($callback = null, int $flag = 0): ArrayedInterface;

Arrayed::reduce($function, $initial = null): ArrayedInterface;

Arrayed::merge(array $array2 = null, ...$_): ArrayedInterface;

Arrayed::mergeRecursive(...$_): ArrayedInterface;

Arrayed::flip(): ArrayedInterface;

Arrayed::intersect(array $array2, ...$_): ArrayedInterface;

Arrayed::values(): ArrayedInterface;

Arrayed::keys($overwrite = true): ArrayedInterface;

Arrayed::offsetGet($offset);

Arrayed::offsetSet($offset, $value): ArrayedInterface;

Arrayed::offsetUnset($offset): ArrayedInterface;

Arrayed::sum(): int;

Arrayed::contains($needle, bool $strict = false): bool;

Arrayed::isArray(): bool;

Arrayed::keyExists($key): bool;

Arrayed::offsetExists($offset): bool;

Arrayed::empty(): bool;

Arrayed::count(): int;

Arrayed::pipe(callable $action, ...$parameters);

Arrayed::result(callable $callable = null);

Arrayed::raw(): array;

Arrayed::initial(): array; // Deprecated, use raw() instead

Arrayed::tap(Closure $closure): ArrayedInterface;

Arrayed::copy(): ArrayedInterface;

Arrayed::collect(...$with): array;

// Other Array_* methods

Arrayed::changeKeyCase(int $case = null): ArrayedInterface;

Arrayed::chunk(int $size, bool $preserve_keys = false): ArrayedInterface;

Arrayed::column($column, $index_key = null): ArrayedInterface;

Arrayed::countValues(): ArrayedInterface;

Arrayed::diffAssoc(array $array2, array ...$_): ArrayedInterface;

Arrayed::diff(array $array2, array ...$_): ArrayedInterface;

Arrayed::reverse(bool $preserve_keys = false): ArrayedInterface;

Arrayed::diffUassoc(callable $key_compare_func, array $array2, array ...$_): ArrayedInterface;

Arrayed::diffKey(array $array2, array ...$_): ArrayedInterface;