group_adjacent

Modified on Thu, 9 Jul at 10:49 AM

group_adjacent

TABLE OF CONTENTS

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

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

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article