{"id":2669,"date":"2020-04-02T07:08:09","date_gmt":"2020-04-02T06:08:09","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=2669"},"modified":"2023-03-20T17:32:37","modified_gmt":"2023-03-20T17:32:37","slug":"manipulating-arrays","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/memory\/arrays\/manipulating-arrays","title":{"rendered":"Manipulating Arrays"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Remove array entry<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/Remove specific index from array\n  unset($MyArray&#91;'SomeName']);\n\n  \/\/Remove first index from array \n  $IndexValue = array_shift($MyArray);\n\n  \/\/Remove last index from array\n  array_pop($MyArray);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Inserting New Entry into array<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">Insert at index 0<\/h4>\n\n\n\n<p>The rest of the array entries move down if there is already a [0] entry<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $Results = db_device_read_all_devices();\n  if ($Results != False)\n  {\n    \/\/Insert our coumn titles into the Results array as index 0 (the rest of the array moves down)\n    $ColumnTitles = array(\n      \"DeviceId\"=&gt;\"Device ID\",\n      \"Name\"=&gt;\"Name\",\n      \"CreatedDateTime\"=&gt;\"Created\",\n      \"LastConnected\"=&gt;\"Last Connected\",\n      \"SoftwareVersion\"=&gt;\"Software Version\",\n      \"Notes\"=&gt;\"Notes\"\n    );\n    array_unshift($Results,$ColumnTitles);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>You can insert mutliple entries at the start of the array if you wish, e.g.\n$MyArray = &#91;\"ab\", \"cd\"];\narray_unshift($MyArray, \"ef\", \"gh\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Sort Array<\/h4>\n\n\n\n<p><a href=\"https:\/\/www.w3schools.com\/php\/php_arrays_sort.asp\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.w3schools.com\/php\/php_arrays_sort.asp<\/a><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Sort by array key<\/h5>\n\n\n\n<p>ksort() sorts an associative array in ascending order, according to the key<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $age = array(\"Peter\"=&gt;\"35\", \"Ben\"=&gt;\"37\", \"Joe\"=&gt;\"43\");\n  ksort($age);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Remove array entry Inserting New Entry into array Insert at index 0 The rest of the array entries move down if there is already a [0] entry Sort Array https:\/\/www.w3schools.com\/php\/php_arrays_sort.asp Sort by array key ksort() sorts an associative array in ascending order, according to the key<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-2669","post","type-post","status-publish","format-standard","hentry","category-arrays"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=2669"}],"version-history":[{"count":9,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2669\/revisions"}],"predecessor-version":[{"id":4492,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2669\/revisions\/4492"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=2669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=2669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=2669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}