Filters an array by a list of filter-properties.

The function name relates to the SQL-Statement "SELECT".

(Although not close to typical SELECT-power and flexibility of common SQL-databases!  ;)  )


Syntax

array_select(?array $arr, ?array $props = null, ?array $filter = null): ?array


Parameter

@param array|null $array The array to filter. The expected array is a 2-dim. data-list-array, meaning: A numerical array of associative arrays (objects).

@param array|null $props Optional list of properties (aka "fields", "columns") to be returned. If NULL, all properties will be returned.

@param array $filter The actual filter. Expected is an associative array with the following structure:
[ "<prop>" => "<match-value>", ... ]
The filter will be applied to each row of the array. You can define multiple filter. All filter must match to return a row (=  AND-condition).
Currently only an "equal"-operation is supported.


Return

Returns the filtered array. Returns NULL on error. The returned numerical array is indexed from 0...n. Original keys from $array are lost.


Example

Yet to come...

{% set arr1 = [
  {
    "id": 1,
    "firstname": "Hubby",
  },
  {
    "id": 2,
    "firstname": "Kevin",
  },
  {
    "id": 3,
    "firstname": "Luke",
  },
] %}

{# --- Filter arr1 for rows where "firstname" == "Luke" and return only property "id" of those. --- #}
{% set result = hublify.array_select(arr1, [ 'id' ], [ { 'firstname': 'Luke' } ]) %}

{% hublify.dump(result) %}