class Arrays (View source)

The GenericDatabase\Helpers\Types\Compounds\Arrays class provides a collection of static methods for manipulating arrays in PHP.

It includes functions for finding elements in an array, combining array indices and values, determining the type of array, and more.

Example Usage:

//Find elements in array except by keys
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$keys = ['a', 'c'];
$result = Arrays::exceptByKeys($array, $keys);

Output: ['b' => 2]

//Find elements in array except by values
$array = ['a', 'b', 'c'];
$values = ['b', 'c'];
$result = Arrays::exceptByValues($array, $values);

Output: ['a']

//Find the first element that matches between two arrays
$list = ['apple', 'banana', 'cherry'];
$array = ['a' => 'apple', 'b' => 'banana', 'c' => 'cherry'];
$result = Arrays::matchValues($list, $array);

Output: 'apple'

//Iterate the array by combining the indices and values into a new array
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$result = Arrays::recombine($array);

Output: ['a' => 1, 'b' => 2, 'c' => 3]

//Iterate through the array combining the values by substituting the indices
//into sequential numbers starting at zero into a new array
$array = ['a', 'b', 'c'];
$result = Arrays::assocToIndex($array);

Output: [0 => 'a', 1 => 'b', 2 => 'c']

//Determine if array is indexed or associative
$array = ['a', 'b', 'c'];
$result = Arrays::isAssoc($array);

Output: false

//Determine if array is multidimensional
$array = [['a' => 1], ['b' => 2]];
$result = Arrays::isMultidimensional($array);

Output: true

//Get array values recursively
$array = ['a' => [1, 2], 'b' => [3, 4]];
$result = Arrays::arrayValuesRecursive($array);

Output: [[1, 2], [3, 4]]

//Create an index or list array to an associative array
$array1 = ['a', 'b', 'c'];
$array2 = ['x' => 1, 'y' => 2, 'z' => 3];
$result = Arrays::assocToIndexCombine($array1, $array2);

Output: [0 => 'a', 'a' => 'a', 1 => 'b', 'b' => 'b', 2 => 'c', 'c' => 'c', 'x' => 1, 'y' => 2, 'z' => 3]

Main functionalities:

  • Finding elements in an array except by keys or values
  • Finding the first element that matches between two arrays
  • Combining array indices and values into a new array
  • Determining if an array is indexed or associative
  • Determining if an array is multidimensional
  • Getting array values recursively
  • Creating an index or list array to an associative array

Methods:

  • exceptByKeys(array $array, array $keys): array: Finds elements in an array except by keys.
  • exceptByValues(array $array, array $values): array: Finds elements in an array except by values.
  • matchValues(array $list, array $array, ?string $apply = 'mb_strtolower'): string: Finds the first element that matches between two arrays.
  • recombine(array $array): array: Combines the indices and values of an array into a new array.
  • assocToIndex(array $array): array: Combines the values of an array by substituting the indices into sequential numbers starting at zero into a new array.
  • isAssoc(mixed $array): bool: Determines if an array is indexed or associative.
  • isMultidimensional(array $array): bool: Determines if an array is multidimensional.
  • arrayValuesRecursive(array $array): array: Gets the array values recursively.
  • assocToIndexCombine(array ...$arrays): array: Creates an index or list array to

Methods

static array
exceptByKeys(array $array, array $keys)

Find elements in array except by keys

static array
exceptByValues(array $array, array $values)

Find elements in array except by values

static string
matchValues(array $list, array $array, string|null $apply = 'mb_strtolower')

Find the first element that matches between two arrays

static array
recombine(array $array)

Iterate the array by combining the indices and values into a new array

static array
assocToIndex(array $array)

Iterates through the array combining the values by substituting the indices into sequential numbers starting at zero into a new array

static bool
isAssoc(mixed $array)

Determine if array is indexed or associative

static bool
isMultidimensional(array|string $array)

Determine if array is an array multidimensional

static int
isDepthArray(array $array)

Determine the depth of a multidimensional array recursively.

static array
arrayValuesRecursive(array $array)

Determine if array is an array multidimensional

static array
assocToIndexCombine(array ...$arrays)

Create a numeric and incremental array, from an associative array, simulating the FETCH_BOTH flag of the fetch or fetchAll method, combining the associative array with the numeric array into a single array.

static array
arrayFlatten(array $array)

Flatten a array

static array
arrayGroupBy(array $arr, string|callable $criteria)

Group items from an array together by some criteria or value.

static array
arraySafe(array $array)

A function that filters out null values from the input array.

Details

static array exceptByKeys(array $array, array $keys)

Find elements in array except by keys

Parameters

array $array

Array to find

array $keys

Keys from array

Return Value

array

static array exceptByValues(array $array, array $values)

Find elements in array except by values

Parameters

array $array

Array to find

array $values

Values from array

Return Value

array

static string matchValues(array $list, array $array, string|null $apply = 'mb_strtolower')

Find the first element that matches between two arrays

Parameters

array $list

A string list array

array $array

An associative array

string|null $apply

Filter to be applied

Return Value

string

static array recombine(array $array)

Iterate the array by combining the indices and values into a new array

Parameters

array $array

The array to combine

Return Value

array

static array assocToIndex(array $array)

Iterates through the array combining the values by substituting the indices into sequential numbers starting at zero into a new array

Parameters

array $array

The array to combine

Return Value

array

static bool isAssoc(mixed $array)

Determine if array is indexed or associative

Parameters

mixed $array

The array

Return Value

bool

static bool isMultidimensional(array|string $array)

Determine if array is an array multidimensional

Parameters

array|string $array

$array The array

Return Value

bool

static int isDepthArray(array $array)

Determine the depth of a multidimensional array recursively.

Parameters

array $array

The array to calculate the depth for.

Return Value

int

The depth of the multidimensional array.

static array arrayValuesRecursive(array $array)

Determine if array is an array multidimensional

Parameters

array $array

The array

Return Value

array

static array assocToIndexCombine(array ...$arrays)

Create a numeric and incremental array, from an associative array, simulating the FETCH_BOTH flag of the fetch or fetchAll method, combining the associative array with the numeric array into a single array.

Parameters

array ...$arrays

The array

Return Value

array

static array arrayFlatten(array $array)

Flatten a array

Parameters

array $array

array The array to flatten items from

Return Value

array

static array arrayGroupBy(array $arr, string|callable $criteria)

Group items from an array together by some criteria or value.

Parameters

array $arr

array The array to group items from

string|callable $criteria

string|callable The key to group by or a function the returns a key to group by.

Return Value

array

static array arraySafe(array $array)

A function that filters out null values from the input array.

Parameters

array $array

The array to filter

Return Value

array

The filtered array with non-null values