{"id":3609,"date":"2022-11-09T10:25:05","date_gmt":"2022-11-09T10:25:05","guid":{"rendered":"https:\/\/info.documotor.com\/?page_id=3609"},"modified":"2023-03-22T11:57:21","modified_gmt":"2023-03-22T11:57:21","slug":"split","status":"publish","type":"page","link":"https:\/\/info.documotor.com\/?page_id=3609","title":{"rendered":"Split"},"content":{"rendered":"\n<p>Split can be used to split an array into two arrays where the split is made on a defined position. The output of the function is an object containing the two new arrays.<\/p>\n\n\n\n<h3>Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">json_object split(array $input, integer $count)<\/code><\/pre>\n\n\n\n<p>Splits $input into two arrays called &#8220;first&#8221; and &#8220;last&#8221; and outputs these in a json object. $count is the number of elements that should be put in the &#8220;first&#8221; array and the &#8220;last&#8221; array contains the rest of the elements. <\/p>\n\n\n\n<h3>Parameters<\/h3>\n\n\n\n<p><strong>$input:<\/strong> The array that should be split into two. <br><strong>$count: <\/strong>The number of elements that should be in the first array. <\/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\">{\n    \"Array\": [1,2,3,4,5,6]\n}<\/code><\/pre>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">{\n    Splitted: split(Array, `4`)\n}<\/code><\/pre>\n\n\n\n<pre title=\"Output\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">{\n    \"Splitted\": {\n        \"first\": [1,2,3,4],\n        \"last\": [5,6]\n    }\n}<\/code><\/pre>\n\n\n\n<p>Above example splits the array of integers on the 4th position (starting from 1) and puts the 4 first elements of the array in the &#8220;first&#8221; array and the rest of the elements in the &#8220;last&#8221; array.<\/p>\n\n\n\n<h3>Notes<\/h3>\n\n\n\n<p><strong>Available types<\/strong><\/p>\n\n\n\n<p>The $input array can contain any type (integer, string, object, array, etc.)<\/p>\n\n\n\n<p>The $count integer can be both positive and negative numbers with followings rules:<\/p>\n\n\n\n<ul><li>If $count is 0 (Zero) it will return all elements in the &#8220;first&#8221; array and the &#8220;last&#8221; array will be empty.<\/li><li>If $count is less than 0 (Negative) it will populate the &#8220;last&#8221; array first and put the rest of the elements in the &#8220;first&#8221; array.<\/li><li>If $count is larger than 0 (Positive) it will pupulate the &#8220;first&#8221; array first and put the rest of the elements in the &#8220;last&#8221; array. <\/li><li>If $count is larger (LargerPositive \/ LargerNegative) than the length of the array all of the elements will be put in either the &#8220;first&#8221; or &#8220;last&#8221; array depending on whether it is positive or negative respectively and the other array will be empty. <\/li><\/ul>\n\n\n\n<p><strong>More examples<\/strong><\/p>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">{\n    Zero: split(Array, `0`),\n    Negative: split(Array, `-4`),\n    Postive: split(Array, `4`),\n    LargerPositive: split(Array, `10`),\n    LargerNegative: split(Array, `-10`)\n}<\/code><\/pre>\n\n\n\n<pre title=\"Output\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">{\n    \"Zero\": {\n        \"first\": [1,2,3,4,5,6],\n        \"last\": []\n    },\n    \"Negative\": {\n        \"first\": [5,6],\n        \"last\": [1,2,3,4]\n    },\n    \"Postive\": {\n        \"first\": [1,2,3,4],\n        \"last\": [4,5,6]\n    },\n    \"LargerPositive\": {\n        \"first\": [1,2,3,4,5,6],\n        \"last\": []\n    },\n    \"LargerNegative\": {\n        \"first\": [],\n        \"last\": [1,2,3,4,5,6]\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Split can be used to split an array into two arrays where the split is made on a defined position. The output of the function is an object containing the two new arrays. Syntax Splits $input into two arrays called &#8220;first&#8221; and &#8220;last&#8221; and outputs these in a json object. $count is the number of [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"parent":271,"menu_order":183,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3609"}],"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\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3609"}],"version-history":[{"count":31,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3609\/revisions"}],"predecessor-version":[{"id":3812,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3609\/revisions\/3812"}],"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=3609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}