{"id":3573,"date":"2022-11-08T09:44:10","date_gmt":"2022-11-08T09:44:10","guid":{"rendered":"https:\/\/info.documotor.com\/?page_id=3573"},"modified":"2023-01-25T10:22:38","modified_gmt":"2023-01-25T10:22:38","slug":"take_or_default","status":"publish","type":"page","link":"https:\/\/info.documotor.com\/?page_id=3573","title":{"rendered":"Take_or_default"},"content":{"rendered":"\n<p>At times, it is necessary to ensure that an array contains an exact number of entries at all times, even if the data for this does not exist in the payload. Using <code>take_or_default<\/code> allows a user to define the exact length of an array, and the default value that should be appended to the array for each item that is missing.<\/p>\n\n\n\n<h3>Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">array take_or_default(array $array, integer $count, any $default)<\/code><\/pre>\n\n\n\n<p>Appends <code>$default<\/code> values to an <code>$array<\/code> where necessary to ensure that the array length is equal to <code>$count<\/code>.<\/p>\n\n\n\n<h3>Parameters<\/h3>\n\n\n\n<p><strong>$array:<\/strong> The input array. <br><strong>$count:<\/strong> The exact length that the output array should be. <br><strong>$default:<\/strong> The default value that should be added to the array for each item that is missing.<\/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    \"AvailableUsers\": [\n        \"Todd\",\n        \"Bob\",\n        \"Steve\"\n    ]\n}<\/code><\/pre>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    listOfUsers: take_or_default(AvailableUsers, '6', 'Unconfirmed User')\n}<\/code><\/pre>\n\n\n\n<pre title=\"Result\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"listOfUsers\": [\n        \"Todd\",\n        \"Bob\",\n        \"Steve\",\n        \"Unconfirmed User\",\n        \"Unconfirmed User\",\n        \"Unconfirmed User\"\n    ]\n}<\/code><\/pre>\n\n\n\n<p>In this example the input <code>$array<\/code> contains only 3 items but the transformation defines that the <code>listOfUsers<\/code> array in the result should have a length of 6 so 3 additional &#8220;Unconfirmed User&#8221; entries are appended to the original array.<\/p>\n\n\n\n<h3>Notes<\/h3>\n\n\n\n<p>The resulting array will ALWAYS have a length equal to <code>$count<\/code>. So if the input <code>$array<\/code> is already equal to or greater than the <code>$count<\/code> defined in the transformation then the resulting array will be truncated appropriately. <\/p>\n\n\n\n<p>For example, if the transformation above was altered to have a count of 2 then the resulting array would show only the first two entries of the input <code>$array<\/code> and no default values would be appended.<\/p>\n\n\n\n<pre title=\"Transformation\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    listOfUsers: take_or_default(AvailableUsers, '2', 'Unconfirmed User')\n}<\/code><\/pre>\n\n\n\n<pre title=\"Result\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n    \"listOfUsers\": [\n        \"Todd\",\n        \"Bob\"\n    ]\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>At times, it is necessary to ensure that an array contains an exact number of entries at all times, even if the data for this does not exist in the payload. Using take_or_default allows a user to define the exact length of an array, and the default value that should be appended to the array [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"parent":271,"menu_order":190,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3573"}],"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\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3573"}],"version-history":[{"count":8,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3573\/revisions"}],"predecessor-version":[{"id":3811,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3573\/revisions\/3811"}],"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=3573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}