2017 © Pedro Peláez
 

library array-utils

Useful functions for working with arrays

image

theodorejb/array-utils

Useful functions for working with arrays

  • Tuesday, May 24, 2016
  • by theodorejb
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1,361 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 5 % Grown

The README.md

ArrayUtils

Packagist Version, (*1)

ArrayUtils is a collection of useful PHP array functions., (*2)

Install via Composer

composer require theodorejb/array-utils, (*3)

Methods

containsAll

Returns true if all the needles are in the haystack., (*4)

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

containsSame

Returns true if both arrays contain the same values (regardless of order)., (*5)

use theodorejb\ArrayUtils\ArrayUtils;

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

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

groupRows

Splits the iterable of arrays into groups when the value of one or more keys changes. The iterable must be sorted by the array keys used to group results., (*6)

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)

requireStrKey

Returns the specified array key value if it is a string. Throws an exception otherwise., (*7)

use theodorejb\ArrayUtils\ArrayUtils;

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

getOptionalStrKey

Returns the specified array key value if it is a string, or null if the array key doesn't exist. Throws an exception if the key exists but the value is not a string., (*8)

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

requireNumericKey

Returns the specified array key value as a float if it is an integer or float. Throws an exception otherwise., (*9)

use theodorejb\ArrayUtils\ArrayUtils;

$data = ['i' => 1, 'f' => 0.5, 'k' => 'val'];
ArrayUtils::requireNumericKey($data, 'i'); // 1.0
ArrayUtils::requireNumericKey($data, 'f'); // 0.5
ArrayUtils::requireNumericKey($data, 'x'); // throws OutOfBoundsException
ArrayUtils::requireNumericKey($data, 'k'); // throws UnexpectedValueException

getOptionalNumericKey

Returns the specified array key value as a float if it is an integer or float, or null if the array key doesn't exist. Throws an exception if the key exists but the value is not an integer or float., (*10)

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

requireIntKey

Returns the specified array key value if it is an integer. Throws an exception otherwise., (*11)

use theodorejb\ArrayUtils\ArrayUtils;

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

getOptionalIntKey

Returns the specified array key value if it is an integer, or null if the array key doesn't exist. Throws an exception if the key exists but the value is not an integer., (*12)

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

requireBoolKey

Returns the specified array key value if it is a boolean. Throws an exception otherwise., (*13)

use theodorejb\ArrayUtils\ArrayUtils;

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

getOptionalBoolKey

Returns the specified array key value if it is a boolean, or null if the array key doesn't exist. Throws an exception if the key exists but the value is not a boolean., (*14)

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

Author

Theodore Brown
https://theodorejb.me, (*15)

License

MIT, (*16)

The Versions

24/05 2016

dev-master

9999999-dev

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Theodore Brown

24/05 2016

v1.1.0

1.1.0.0

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Theodore Brown

14/01 2016

v1.0.1

1.0.1.0

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Theodore Brown

14/01 2016

v1.0.0

1.0.0.0

Useful functions for working with arrays

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Theodore Brown