{"id":3378,"date":"2022-10-24T10:56:53","date_gmt":"2022-10-24T10:56:53","guid":{"rendered":"https:\/\/info.documotor.com\/?page_id=3378"},"modified":"2023-03-22T12:12:57","modified_gmt":"2023-03-22T12:12:57","slug":"group_adjacent","status":"publish","type":"page","link":"https:\/\/info.documotor.com\/?page_id=3378","title":{"rendered":"Group_adjacent"},"content":{"rendered":"\n<p>Takes an array and groups it into multiple arrays based on &amp;groupExpression value match. <\/p>\n\n\n\n<h3>Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">array group_adjacent(array $entry, &amp;groupExpression)<\/code><\/pre>\n\n\n\n<p>Groups $entry, outputting multiple arrays based on adjacency and expression match. <\/p>\n\n\n\n<h3>Parameters<\/h3>\n\n\n\n<p><strong>$entry:<\/strong> The array to group.<br><strong>&amp;groupExpression<\/strong>: The value to group by if adjacent items match.<\/p>\n\n\n\n<h3>Example<\/h3>\n\n\n\n<pre title=\"Data\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"array\": [\n        { \"key\": \"a\" },\n        { \"key\": \"b\" },\n        { \"key\": \"b\" },\n        { \"key\": \"a\" },\n    ]\n}<\/code><\/pre>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">{\n    GroupedArray: group_adjacent(array, &amp;key)\n}<\/code><\/pre>\n\n\n\n<pre title=\"Output\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"GroupedArray\": [\n        [\n            { \"key\": \"a\" }\n        ],\n        [\n            { \"key\": \"b\" },\n            { \"key\": \"b\" }\n        ],\n        [\n            { \"key\": \"a\" }\n        ]\n    ]\n}<\/code><\/pre>\n\n\n\n<p>In this example, the array input is grouped by the &#8220;key&#8221; value. This yields an array of three arrays.<\/p>\n\n\n\n<p>The first array contains a single object as there are no adjacent objects in the input array that match the key value.<\/p>\n\n\n\n<p>The second array contains two objects as there are two adjacent objects in the input array that share the key value &#8220;b&#8221;.<\/p>\n\n\n\n<p>The third array again contains a single object as, even though there are two keys in the input array that share the key value &#8220;a&#8221;, they are not adjacent.<\/p>\n\n\n\n<h3>Notes<\/h3>\n\n\n\n<p><strong>Synergy with sort_by function<\/strong><\/p>\n\n\n\n<p>In order to group all matching keys in an array, group_adjacent can be used on an already sorted array.<\/p>\n\n\n\n<p>Expanding on above example, the following transformation will yield an output of arrays that groups all keys in the input array, because adjacency is ensured by the sort_by function.<\/p>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash line-numbers\">{\n    GroupedArray: group_adjacent(sort_by(array, &amp;key), &amp;key)\n}<\/code><\/pre>\n\n\n\n<pre title=\"Output\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"GroupedArray\": [\n        [\n            { \"key\": \"a\" },\n            { \"key\": \"a\" }\n        ],\n        [\n            { \"key\": \"b\" },\n            { \"key\": \"b\" }\n        ]\n    ]\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Takes an array and groups it into multiple arrays based on &amp;groupExpression value match. Syntax Groups $entry, outputting multiple arrays based on adjacency and expression match. Parameters $entry: The array to group.&amp;groupExpression: The value to group by if adjacent items match. Example In this example, the array input is grouped by the &#8220;key&#8221; value. This [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"parent":271,"menu_order":62,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3378"}],"collection":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3378"}],"version-history":[{"count":5,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3378\/revisions"}],"predecessor-version":[{"id":3833,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3378\/revisions\/3833"}],"up":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/271"}],"wp:attachment":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}