PHP code example of tezmanian / utils-arr

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

    

tezmanian / utils-arr example snippets


use Tezmanian\Utils\Arr\ArrayDiff;

$result = ArrayDiff::compare(
    ['user' => ['name' => 'Alice', 'age' => 30]],
    ['user' => ['name' => 'Bob',   'age' => 30, 'city' => 'Berlin']],
);

$result->hasChanges(); // true

foreach ($result->changes() as $change) {
    // $change->path  → 'user.name'
    // $change->type  → ChangeType::Changed
    // $change->old   → 'Alice'
    // $change->new   → 'Bob'
}

$result->paths(); // ['user.name', 'user.city']

use Tezmanian\Utils\Arr\CartesianProduct;

foreach (CartesianProduct::create(['S', 'M', 'L'], ['red', 'blue']) as $tuple) {
    // ['S', 'red'], ['S', 'blue'], ['M', 'red'], ...
}

use Tezmanian\Utils\Arr\DeepMerge;

$result = DeepMerge::merge(
    ['db' => ['host' => 'localhost', 'port' => 3306]],
    ['db' => ['host' => 'prod.example.com']],
);
// ['db' => ['host' => 'prod.example.com', 'port' => 3306]]

use Tezmanian\Utils\Arr\Find;

$first = Find::first($users, fn($u) => $u['active']);
$last  = Find::last($users,  fn($u) => $u['active']);
// Returns null when nothing matches

use Tezmanian\Utils\Arr\Flatten;

Flatten::flatten([1, [2, [3, 4]]]);           // [1, 2, 3, 4]
Flatten::flatten([1, [2, [3, 4]]], depth: 1); // [1, 2, [3, 4]]

use Tezmanian\Utils\Arr\GroupBy;

$grouped = GroupBy::group($orders, fn($o) => $o['status']);
// ['pending' => [...], 'shipped' => [...]]

use Tezmanian\Utils\Arr\KeyBy;

$byId = KeyBy::index($users, fn($u) => $u['id']);
// [42 => $user, 99 => $user, ...]

use Tezmanian\Utils\Arr\MapWithKeys;

$result = MapWithKeys::map($products, fn($p) => [$p['sku'] => $p['price']]);
// ['ABC-1' => 9.99, 'XYZ-2' => 4.49]

use Tezmanian\Utils\Arr\Paginator;

$page = Paginator::paginate($items, page: 2, perPage: 15);

$page->items();       // items on this page
$page->total();       // total item count
$page->currentPage(); // 2
$page->lastPage();    // e.g. 7
$page->hasNext();     // true / false
$page->hasPrev();     // true

use Tezmanian\Utils\Arr\Partition;

[$active, $inactive] = Partition::by($users, fn($u) => $u['active']);

use Tezmanian\Utils\Arr\Pluck;

// Extract a single column
Pluck::values($users, 'name'); // ['Alice', 'Bob', ...]

// Build key → value map
Pluck::keyedBy($users, valueKey: 'name', indexKey: 'id');
// [42 => 'Alice', 99 => 'Bob']

// Dot-notation for nested values
Pluck::values($orders, 'address.city');

use Tezmanian\Utils\Arr\SafeGet;

$data = ['chart' => ['result' => [['meta' => ['currency' => 'EUR']]]]];

SafeGet::string($data, 'chart.result.0.meta.currency'); // 'EUR'
SafeGet::int($data,    'chart.result.0.meta.missing');  // null
SafeGet::float($data,  'price', default: 0.0);
SafeGet::bool($data,   'flags.active');
SafeGet::array($data,  'chart.result');

use Tezmanian\Utils\Arr\Sliding;

Sliding::window([1, 2, 3, 4, 5], size: 3);
// [[1,2,3], [2,3,4], [3,4,5]]

Sliding::window([1, 2, 3, 4, 5], size: 3, step: 2);
// [[1,2,3], [3,4,5]]

use Tezmanian\Utils\Arr\SortBy;

$sorted = SortBy::sort($products, fn($p) => $p['price']);
$desc   = SortBy::sort($products, fn($p) => $p['price'], descending: true);

use Tezmanian\Utils\Arr\Transpose;

Transpose::matrix([[1, 2, 3], [4, 5, 6]]);
// [[1, 4], [2, 5], [3, 6]]

use Tezmanian\Utils\Arr\UniqueBy;

// Keep first occurrence (default)
UniqueBy::filter($items, fn($i) => $i['email']);

// Keep last occurrence
UniqueBy::filter($items, fn($i) => $i['email'], lastWins: true);

// Preserve original keys
UniqueBy::filter($items, fn($i) => $i['email'], preserveKeys: true);

use Tezmanian\Utils\Arr\Wrap;

Wrap::ensure(null);    // []
Wrap::ensure('hello'); // ['hello']
Wrap::ensure([1, 2]);  // [1, 2]

use Tezmanian\Utils\Arr\Zip;

// Pad shorter arrays with null
Zip::zip([1, 2, 3], ['a', 'b']);
// [[1, 'a'], [2, 'b'], [3, null]]

// Stop at shortest input
Zip::shortest([1, 2, 3], ['a', 'b']);
// [[1, 'a'], [2, 'b']]