group_adjacent

Group adjacent elements that share a property value

Definition

array group_adjacent(array $entry, &groupExpression)

Groups adjacent elements of an input array based on matching values assigned to the &groupExpression key.

Parameters

array $entry

Array to split into groups.

string &groupExpression

Key name to group by.

Returns

array

Array $entry grouped into subarrays, where each subarray is made up of all contiguous elements sharing the same value of $groupExpression.

Examples

{
  "array": [
    {"key": "a"},
    {"key": "b"},
    {"key": "b"},
    {"key": "a"}
  ]
}
{
    group: group_adjacent(array, &key)
}
{
  "group": [
    [{"key": "a"}],
    [{"key": "b"}, {"key": "b"}],
    [{"key": "a"}]
  ]
}