Skip to content

Wiki Iterables

Danijel Galić edited this page Feb 1, 2024 · 2 revisions
final class \FireHub\Core\Support\LowLevel\Iterables()

Important

This class is marked as final.

### Array or Traversable low-level proxy class

This class was created by Danijel Galić <danijel.galic@outlook.com>
Copyright: 2024 FireHub Web Application Framework
License: <https://opensource.org/licenses/OSL-3.0> OSL Open Source License version 3
Version: GIT: $Id$ Blob checksum.

Fully Qualified Class Name:  \FireHub\Core\Support\LowLevel\Iterables
Source code:  view source code
Blame:  view blame
History:  view history

Methods

Type Name Title
public static count ### Counts all elements in the array
public static current ### Return the current element in an array
public static key ### Fetch a key from an array
public static prev ### Rewind the internal array pointer
public static next ### Advance the internal pointer of an array
public static reset ### Set the internal pointer of an array to its first element
public static end ### Set the internal pointer of an array to its last element
public static Iterables::count(array|\Countable $array, bool $multidimensional = false):int

### Counts all elements in the array

Source code:  view source code
Blame:  view blame

Parameters

  • array or \Countable $array - array<array-key, mixed> Array to count.
  • bool $multidimensional = false - [optional] Count multidimensional items.

Returns

  • int - non-negative-int Number of elements in an array.
public static Iterables::current(array $array):mixed

Warning

This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.> [!NOTE] The results of calling current() on an empty array and on an array, whose internal pointer points beyond the end of the elements is indistinguishable from a bool false element. To properly traverse an array which may contain false elements, see the foreach control structure. To still use current() and properly check if the value is really an element of the array, the key() of the current() element should be checked to be strictly different from null.

### Return the current element in an array

Every array has an internal pointer to its "current" element, which is initialized to the first element inserted into the array.

Source code:  view source code
Blame:  view blame

Templates

  • TKey of array-key
  • TValue

Parameters

  • array $array - array<TKey, TValue> The array.

Returns

  • mixed - TValue|false The current() function simply returns the value of the array element that is being pointed to with the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, current() returns false.
public static Iterables::key(array $array):null|int|string

### Fetch a key from an array

Key returns the index element of the current array position.

Source code:  view source code
Blame:  view blame

Templates

  • TKey of array-key
  • TValue

Parameters

  • array $array - array<TKey, TValue> The array.

Returns

  • null or int or string - TKey|null The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, key() returns null.
public static Iterables::prev(array &$array):mixed

Warning

This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.> [!NOTE] The beginning of an array is indistinguishable from a bool false element. To make the distinction, check that the key() of the prev() element is not null.

### Rewind the internal array pointer

Method prev() behaves just like next(), except it rewinds the internal array pointer one place instead of advancing it.

Source code:  view source code
Blame:  view blame

Templates

  • TKey of array-key
  • TValue

Parameters

  • by reference array $array - array<TKey, TValue> The input array.

Returns

  • mixed - TValue|false Returns the array value in the previous place that's pointed to by the internal array pointer, or false if there are no more elements.
public static Iterables::next(array &$array):mixed

Warning

This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.> [!NOTE] The end of an array is indistinguishable from a bool false element. To properly traverse an array which may contain false elements, see the foreach function. To still use next() and properly check if the end of the array has been reached, verify that the key() is null.

### Advance the internal pointer of an array

Method next() behaves like current(), with one difference. It advances the internal array pointer one place forward before returning the element value. That means it returns the next array value and advances the internal array pointer by one.

Source code:  view source code
Blame:  view blame

Templates

  • TKey of array-key
  • TValue

Parameters

  • by reference array $array - array<TKey, TValue> The array being affected.

Returns

  • mixed - TValue|false Returns the array value in the next place that's pointed to by the internal array pointer, or false if there are no more elements.
public static Iterables::reset(array &$array):mixed

Warning

This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.> [!NOTE] The return value for an empty array is indistinguishable from the return value in case of an array which has a bool false first element. To properly check the value of the first element in an array which may contain false elements, first check the count() of the array, or check that key() is not null, after calling reset().

### Set the internal pointer of an array to its first element

Method reset() rewinds array's internal pointer to the first element and returns the value of the first array element.

Source code:  view source code
Blame:  view blame

Templates

  • TKey of array-key
  • TValue

Parameters

  • by reference array $array - &array<TKey, TValue> The input array.

Returns

  • mixed - TValue|false Returns the value of the first array element, or false if the array is empty.
public static Iterables::end(array &$array):mixed

Warning

This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

### Set the internal pointer of an array to its last element

Method end() advances array's internal pointer to the last element, and returns its value.

Source code:  view source code
Blame:  view blame

Templates

  • TKey of array-key
  • TValue

Parameters

  • by reference array $array - &array<TKey, TValue> The input array.

Returns

  • mixed - TValue|false Returns the value of the last element or false for an empty array.
Clone this wiki locally