PHP code example of theodorejb / array-utils

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

    

theodorejb / array-utils example snippets


use theodorejb\ArrayUtils\ArrayUtils;

$haystack = [1, 2, 3, 5, 8, 13];
$needles = [2, 13, 5];
ArrayUtils::containsAll($needles, $haystack); // true
ArrayUtils::containsAll($haystack, $needles); // false

use theodorejb\ArrayUtils\ArrayUtils;

$set1 = [1, 3, 5, 7];
$set2 = [3, 7, 5, 1];

ArrayUtils::containsSame($set1, $set2); // true

use theodorejb\ArrayUtils\ArrayUtils;

$peoplePets = [
    ['lName' => 'Jordan', 'fName' => 'Jack', 'pet' => 'Scruffy'],
    ['lName' => 'Jordan', 'fName' => 'Jack', 'pet' => 'Spot'],
    ['lName' => 'Jordan', 'fName' => 'Jill', 'pet' => 'Paws'],
    ['lName' => 'Greene', 'fName' => 'Amy',  'pet' => 'Blackie'],
    ['lName' => 'Greene', 'fName' => 'Amy',  'pet' => 'Whiskers'],
    ['lName' => 'Greene', 'fName' => 'Amy',  'pet' => 'Paws'],
    ['lName' => 'Smith',  'fName' => 'Amy',  'pet' => 'Tiger'],
];

$grouped = [];

foreach (ArrayUtils::groupRows($peoplePets, 'fName') as $group) {
    $grouped[] = $group;
}

$expected = [
    [$peoplePets[0], $peoplePets[1]],
    [$peoplePets[2]],
    [$peoplePets[3], $peoplePets[4], $peoplePets[5], $peoplePets[6]],
];

var_dump($grouped === $expected); // bool(true)

////// Additional arguments can be passed to `groupRows` to group by more than one column:

$grouped = [];

foreach (ArrayUtils::groupRows($peoplePets, 'lName', 'fName') as $group) {
    $grouped[] = $group;
}

$expected = [
    [$peoplePets[0], $peoplePets[1]],
    [$peoplePets[2]],
    [$peoplePets[3], $peoplePets[4], $peoplePets[5]],
    [$peoplePets[6]],
];

var_dump($grouped === $expected); // bool(true)

use theodorejb\ArrayUtils\ArrayUtils;

$data = ['k' => 'val', 'i' => 1];
ArrayUtils::quireStrKey($data, 'i'); // throws UnexpectedValueException

use theodorejb\ArrayUtils\ArrayUtils;

$data = ['k' => 'val', 'i' => 1];
ArrayUtils::getOptionalStrKey($data, 'k'); // val
ArrayUtils::getOptionalStrKey($data, 'x'); // null
ArrayUtils::getOptionalStrKey($data, 'i'); // throws UnexpectedValueException

use theodorejb\ArrayUtils\ArrayUtils;

$data = ['i' => 1, 'f' => 0.5, 'k' => 'val'];
ArrayUtils::throws OutOfBoundsException
ArrayUtils::

use theodorejb\ArrayUtils\ArrayUtils;

$data = ['i' => 2, 'f' => 0.5, 'k' => 'val'];
ArrayUtils::getOptionalNumericKey($data, 'i'); // 2.0
ArrayUtils::getOptionalNumericKey($data, 'f'); // 0.5
ArrayUtils::getOptionalNumericKey($data, 'x'); // null
ArrayUtils::getOptionalNumericKey($data, 'k'); // throws UnexpectedValueException

use theodorejb\ArrayUtils\ArrayUtils;

$data = ['k' => 'val', 'i' => 1];
ArrayUtils::quireIntKey($data, 'k'); // throws UnexpectedValueException

use theodorejb\ArrayUtils\ArrayUtils;

$data = ['k' => 'val', 'i' => 2];
ArrayUtils::getOptionalIntKey($data, 'i'); // 2
ArrayUtils::getOptionalIntKey($data, 'x'); // null
ArrayUtils::getOptionalIntKey($data, 'k'); // throws UnexpectedValueException

use theodorejb\ArrayUtils\ArrayUtils;

$data = ['k' => 'val', 'b' => true];
ArrayUtils::ireBoolKey($data, 'k'); // throws UnexpectedValueException

use theodorejb\ArrayUtils\ArrayUtils;

$data = ['k' => 'val', 'b' => false];
ArrayUtils::getOptionalBoolKey($data, 'b'); // false
ArrayUtils::getOptionalBoolKey($data, 'x'); // null
ArrayUtils::getOptionalBoolKey($data, 'k'); // throws UnexpectedValueException