Array

Extended version of Laravel's Arr componentopen in new window.

randomElement()

Deprecated

The randomElement() is deprecated. Pleas use Arr::randomizer()->value() instead.

Returns a single random element from a given list.

use Aedart\Utils\Arr;

$element = Arr::randomElement(['Jim', 'Sine', 'Ally', 'Gordon']);

See also Laravel's Arr::randomopen in new window.

Seeding

You can also provide a seed for the random number generatoropen in new window.

use Aedart\Utils\Arr;
use Aedart\Utils\Math;

$element = Arr::randomElement(
    ['Jim', 'Sine', 'Ally', 'Gordon'],
    Math::seed(),
    MT_RAND_PHP
);

See Math::seed() and Math::applySeed() methods for additional information about seeding.

randomizer()

The randomizer() method returns a ArrayRandomizer component - an adapter for PHP Random\Randomizeropen in new window.

use Aedart\Utils\Arr;

$randomizer = Arr::randomizer();

You can optionally specify what Engineopen in new window you wish to use:

use Aedart\Utils\Arr;
use Random\Engine\Mt19937;

$randomizer = Arr::randomizer(new Mt19937());

pickKey()

Returns a single random key.

$arr = [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 ];

Arr::randomizer()->pickKey($arr); // c

pickKeys()

Returns random array keys.

$arr = [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 ];

Arr::randomizer()->pickKeys($arr, 2); // [ 'b', 'd' ]

See Random\Randomizer::pickArrayKeys()open in new window for details.

value()

Returns a random entry (value) from array.

$arr = [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 ];

Arr::randomizer()->value($arr); // 4

values()

Returns random entries (values) from array.

$arr = [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 ];

Arr::randomizer()->values($arr, 2); // [ 2, 5 ]

You can pass true as the third argument, if you wish to preserve the keys.

$arr = [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 ];

Arr::randomizer()->values($arr, 2, true); // [ 'b' => 2, 'e' => 5 ]

shuffle()

Shuffles given array.

$arr = [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 ];

Arr::randomizer()->shuffle($arr); // [ 2, 1, 4, 3, 5 ]

Caution

shuffle() does NOT preserve the array keys.

See Random\Randomizer::shuffleArray()open in new window for details.

differenceAssoc()

Method computes the difference of two or more multidimensional arrays.

use Aedart\Utils\Arr;

$original = [
    'key' => 'person',
    'value' => 'John Snow',
    'settings' => [
        'validation' => [
            'required' => true,
            'nullable' => true,
            'min' => 2,
            'max' => 50,
        ]
    ]
];

$changed = [
    'key' => 'person',
    'value' => 'Smith Snow', // Changed
    'settings' => [
        'validation' => [
            'required' => false, // Changed
            'nullable' => true,
            'min' => 2,
            'max' => 100, // Changed
        ]
    ]
];

$result = Arr::differenceAssoc($original, $changed);
print_r($result);

The output of the above shown example will be the following:

Array
(
  ['value'] => 'John Snow'
  ['settings'] => Array
      (
          ['validation'] => Array
              (
                  ['required'] => 1
                  ['max'] => 50
              )
      )
)

tree()

Returns an array that represents a "tree" structure for given path.

use Aedart\Utils\Arr;

$result = Arr::tree('/home/user/projects');

print_r($result);
Array
(
    [0] => /home
    [1] => /home/user
    [2] => /home/user/projects
)