{"id":1856,"date":"2017-03-23T16:47:21","date_gmt":"2017-03-23T11:17:21","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1856"},"modified":"2017-03-28T18:09:15","modified_gmt":"2017-03-28T12:39:15","slug":"insert-new-item-array-position-php","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/insert-new-item-array-position-php\/","title":{"rendered":"[ Solved &#8211; 10 Answers ] PHP &#8211; Insert new item in array on any position in PHP"},"content":{"rendered":"<p><label class=\"label label-Warning\">PROBLEM :<\/label><\/p>\n<p>How can we insert a new item into an array on any position, say for example in the middle of array?<\/p>\n<p>or<\/p>\n<p>How can we insert a new item into an array on any position?<\/p>\n<p>php arrays insert<\/p>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<h4 id=\"it-only-requires-one-function-call-to-array_splice\"><strong><span style=\"color: #ff6600;\">It only requires one function call to array_splice:<\/span><\/strong><\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;%24original%20%3D%20array(%20%E2%80%98w&#8217;%2C%20%E2%80%98i&#8217;%2C%20%E2%80%98k&#8217;%2C%20%E2%80%98e&#8217;%2C%20%E2%80%98t&#8217;%20)%3B%0A%24inserted%20%3D%20array(%20&#8217;x&#8217;%20)%3B%20%2F%2F%20Not%20necessarily%20an%20array%0A%0Aarray_splice(%20%24original%2C%203%2C%200%2C%20%24inserted%20)%3B%20%2F%2F%20splice%20in%20at%20position%203%0A%2F%2F%20%24original%20is%20now%20w%20i%20k%20x%20e%20t%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<h4 id=\"here-is-the-solution-for-insert-arrays\"><strong><span style=\"color: #808000;\">Here is the solution for insert arrays:<\/span><\/strong><\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;function%20array_insert(%26%24array%2C%20%24value%2C%20%24index)%0A%7B%0A%20%20%20%20return%20%24array%20%3D%20array_merge(array_splice(%24array%2C%20max(0%2C%20%24index%20-%201))%2C%20array(%24value)%2C%20%24array)%3B%0A%7D%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<h4 id=\"a-function-that-can-insert-at-both-integer-and-string-positions\"><strong><span style=\"color: #800080;\">A function that can insert at both integer and string positions:<\/span><\/strong><\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;%2F**%0A%20*%20%40param%20array%20%20%20%20%20%20%24array%0A%20*%20%40param%20int%7Cstring%20%24position%0A%20*%20%40param%20mixed%20%20%20%20%20%20%24insert%0A%20*%2F%0Afunction%20array_insert(%26%24array%2C%20%24position%2C%20%24insert)%0A%7B%0A%20%20%20%20if%20(is_int(%24position))%20%7B%0A%20%20%20%20%20%20%20%20array_splice(%24array%2C%20%24position%2C%200%2C%20%24insert)%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%24pos%20%20%20%3D%20array_search(%24position%2C%20array_keys(%24array))%3B%0A%20%20%20%20%20%20%20%20%24array%20%3D%20array_merge(%0A%20%20%20%20%20%20%20%20%20%20%20%20array_slice(%24array%2C%200%2C%20%24pos)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%24insert%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20array_slice(%24array%2C%20%24pos)%0A%20%20%20%20%20%20%20%20)%3B%0A%20%20%20%20%7D%0A%7D%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h4 id=\"integer-usage\"><strong><span style=\"color: #ff6600;\">Integer usage:<\/span><\/strong><\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;%24arr%20%3D%20%5B%22one%22%2C%20%22two%22%2C%20%22three%22%5D%3B%0Aarray_insert(%0A%20%20%20%20%24arr%2C%0A%20%20%20%201%2C%0A%20%20%20%20%22one-half%22%0A)%3B%0A%2F%2F%20-%3E%0Aarray%20(%0A%20%200%20%3D%3E%20&#8217;one&#8217;%2C%0A%20%201%20%3D%3E%20&#8217;one-half&#8217;%2C%0A%20%202%20%3D%3E%20&#8217;two&#8217;%2C%0A%20%203%20%3D%3E%20&#8217;three&#8217;%2C%0A)%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h4 id=\"string-usage\"><strong><span style=\"color: #808000;\">String Usage:<\/span><\/strong><\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;%24arr%20%3D%20%5B%0A%20%20%20%20%22name%22%20%20%3D%3E%20%5B%0A%20%20%20%20%20%20%20%20%22type%22%20%20%20%20%20%20%3D%3E%20%22string%22%2C%0A%20%20%20%20%20%20%20%20%22maxlength%22%20%3D%3E%20%2240%22%2C%0A%20%20%20%20%5D%2C%0A%20%20%20%20%22email%22%20%3D%3E%20%5B%0A%20%20%20%20%20%20%20%20%22type%22%20%20%20%20%20%20%3D%3E%20%22email%22%2C%0A%20%20%20%20%20%20%20%20%22maxlength%22%20%3D%3E%20%22160%22%2C%0A%20%20%20%20%5D%2C%0A%5D%3B%0A%0Aarray_insert(%0A%20%20%20%20%24arr%2C%0A%20%20%20%20%22email%22%2C%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%22phone%22%20%3D%3E%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22type%22%20%20%20%3D%3E%20%22string%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22format%22%20%3D%3E%20%22phone%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%5D%0A)%3B%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[pastacode lang=&#8221;php&#8221; manual=&#8221;%2F%2F%20-%3E%0Aarray%20(%0A%20%20&#8217;name&#8217;%20%3D%3E%0A%20%20array%20(%0A%20%20%20%20&#8217;type&#8217;%20%3D%3E%20&#8217;string&#8217;%2C%0A%20%20%20%20&#8217;maxlength&#8217;%20%3D%3E%20&#8217;40&#8217;%2C%0A%20%20)%2C%0A%20%20&#8217;phone&#8217;%20%3D%3E%0A%20%20array%20(%0A%20%20%20%20&#8217;type&#8217;%20%3D%3E%20&#8217;string&#8217;%2C%0A%20%20%20%20&#8217;format&#8217;%20%3D%3E%20&#8217;phone&#8217;%2C%0A%20%20)%2C%0A%20%20&#8217;email&#8217;%20%3D%3E%0A%20%20array%20(%0A%20%20%20%20&#8217;type&#8217;%20%3D%3E%20&#8217;email&#8217;%2C%0A%20%20%20%20&#8217;maxlength&#8217;%20%3D%3E%20&#8217;160&#8217;%2C%0A%20%20)%2C%0A)%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<h4 id=\"we-can-use-this\"><strong><span style=\"color: #800080;\">We can use this:<\/span><\/strong><\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;foreach%20(%24array%20as%20%24key%20%3D%3E%20%24value)%20%0A%7B%0A%20%20%20%20if(%24key%3D%3D1)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24new_array%5B%5D%3D%24other_array%3B%0A%20%20%20%20%7D%20%20%20%0A%20%20%20%20%24new_array%5B%5D%3D%24value%3B%20%20%20%20%0A%7D%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<h4 id=\"here-is-the-another-solution\"><span style=\"color: #ff6600;\"><strong>Here is the another Solution:\u00a0&#8211;<\/strong><\/span><\/h4>\n<h4 id=\"php-code\"><strong><span style=\"color: #808000;\">PHP Code:<\/span><\/strong><\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;%3C%3Fphp%20%20%0A%24original%20%3D%20array(%20&#8217;1&#8217;%2C&#8217;2&#8217;%2C&#8217;3&#8217;%2C&#8217;4&#8217;%2C&#8217;5&#8217;%20)%3B%20%20%0Aecho%20&#8217;Original%20array%20%3A%20&#8242;.%22%5Cn%22%3B%20%20%0Aforeach%20(%24original%20as%20%24x)%20%20%20%0A%7Becho%20%22%24x%20%22%3B%7D%20%20%0A%24inserted%20%3D%20&#8217;%24&#8217;%3B%20%20%0Aarray_splice(%20%24original%2C%203%2C%200%2C%20%24inserted%20)%3B%20%20%20%0Aecho%20%22%20%5Cn%20After%20inserting%20&#8217;%24&#8217;%20the%20array%20is%20%3A%20%22.%22%5Cn%22%3B%20%20%0Aforeach%20(%24original%20as%20%24x)%20%20%20%0A%7Becho%20%22%24x%20%22%3B%7D%20%20%0Aecho%20%22%5Cn%22%20%20%0A%3F%3E%20%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h4 id=\"sample-output\"><strong><span style=\"color: #800080;\">Sample Output:<\/span><\/strong><\/h4>\n<p><strong>Original array :<\/strong><br \/>\n1 2 3 4 5<br \/>\n<strong>After inserting &#8216;$&#8217; the array is<\/strong> :<br \/>\n1 2 3 $ 4 5<\/p>\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<ul>\n<li>splice method can be used for adding and\/or removing elements from an array.<\/li>\n<li>The first argument specifies the location at which to begin adding or removing elements.<\/li>\n<li>The second argument specifies the number of elements to delete.<\/li>\n<li>When using splice to add elements to an array, the second argument would be zero.<\/li>\n<li>The third and subsequent arguments are elements to be added to the array.<\/li>\n<\/ul>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;var%20ar%20%3D%20%5B1%2C%202%2C%203%2C%204%2C%205%2C%206%5D%3B%0A%2F%2F%20arguments%3A%20start%20position%2C%20number%20of%20elements%20to%20delete%2C%20elements%20to%20add%0Aar.splice(3%2C%200%2C%20%E2%80%98d&#8217;%2C%20%E2%80%98e&#8217;%2C%20%E2%80%98f&#8217;)%3B%0A%0Aconsole.log(%20ar%20)%3B%20%2F%2F%20%5B1%2C%202%2C%203%2C%20%E2%80%9Cd%22%2C%20%E2%80%9Ce%22%2C%20%E2%80%9Cf%22%2C%204%2C%205%2C%206%5D%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n<h4 id=\"hint-for-adding-an-element-at-the-beginning-of-an-array\"><strong><span style=\"color: #ff6600;\">Hint for adding an element at the beginning of an array:<\/span><\/strong><\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;%24a%20%3D%20array(&#8216;first&#8217;%2C%20&#8217;second&#8217;)%3B%0A%24a%5B-1%5D%20%3D%20&#8217;i%20am%20the%20new%20first%20element&#8217;%3B%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h4 id=\"then\"><strong><span style=\"color: #808000;\">then:<\/span><\/strong><\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;foreach(%24a%20as%20%24aelem)%0A%20%20%20%20echo%20%24a%20.%20&#8217;%20&#8217;%3B%0A%2F%2Freturns%20first%2C%20second%2C%20i%20am&#8230;%0A%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h4 id=\"but\"><strong><span style=\"color: #800080;\">but:<\/span><\/strong><\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;for%20(%24i%20%3D%20-1%3B%20%24i%20%3C%20count(%24a)-1%3B%20%24i%2B%2B)%0A%20%20%20%20%20echo%20%24a%20.%20&#8217;%20&#8217;%3B%0A%2F%2Freturns%20i%20am%20as%201st%20element%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p><label class=\"label label-info\">SOLUTION 8:<\/label><\/p>\n<ul>\n<li>Add Elements to the Beginning of an Array:<\/li>\n<li>unshift method is used to add elements to the beginning of an array.<\/li>\n<li>It accepts multiple arguments, adjusts the indexes of existing elements, and returns the new length of the array.<\/li>\n<li>The unshift method modifies the array on which it is invoked.<\/li>\n<\/ul>\n<h4 id=\"first-we-invoke-unshift-passing-a-single-argument-then-multiple-arguments-displaying-the-results-using-console-log\">First we invoke unshift passing a single argument, then multiple arguments, displaying the results using console.log:<\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;var%20ar%20%3D%20%5B&#8217;one&#8217;%2C%20&#8217;two&#8217;%2C%20&#8217;three&#8217;%5D%3B%0A%2F%2F%20add%20single%20element%0Aar.unshift(&#8216;zero&#8217;)%3B%0Aconsole.log(%20ar%20)%3B%20%2F%2F%20%5B%22zero%22%2C%20%22one%22%2C%20%22two%22%2C%20%22three%22%5D%0A%0A%2F%2F%20add%20multiple%20elements%0Aar.unshift(0%2C%201%2C%202%2C%203)%3B%0Aconsole.log(%20ar%20)%3B%20%2F%2F%20%5B0%2C%201%2C%202%2C%203%2C%20%22zero%22%2C%20%22one%22%2C%20%22two%22%2C%20%22three%22%5D%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p><label class=\"label label-info\">SOLUTION 9:<\/label><\/p>\n<h4 id=\"here-is-simple-function-for-insert-new-element-after-a-specific-key-while-preserving-integer-keys\"><strong>Here is simple function for insert new element after a specific key, while preserving integer keys:<\/strong><\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;private%20function%20arrayInsertAfterKey(%24array%2C%20%24afterKey%2C%20%24key%2C%20%24value)%7B%0A%20%20%20%20%24pos%20%20%20%3D%20array_search(%24afterKey%2C%20array_keys(%24array))%3B%0A%0A%20%20%20%20return%20array_merge(%0A%20%20%20%20%20%20%20%20array_slice(%24array%2C%200%2C%20%24pos%2C%20%24preserve_keys%20%3D%20true)%2C%0A%20%20%20%20%20%20%20%20array(%24key%3D%3E%24value)%2C%0A%20%20%20%20%20%20%20%20array_slice(%24array%2C%20%24pos%2C%20%24preserve_keys%20%3D%20true)%0A%20%20%20%20)%3B%0A%7D%20%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p><label class=\"label label-info\">SOLUTION 10:<\/label><\/p>\n<h4 id=\"normally-with-scalar-values\"><strong><span style=\"color: #ff6600;\">Normally, with scalar values:<\/span><\/strong><\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;%24elements%20%3D%20array(&#8216;foo&#8217;%2C%20&#8230;)%3B%0Aarray_splice(%24array%2C%20%24position%2C%20%24length%2C%20%24elements)%3B%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h4 id=\"to-insert-a-single-array-element-into-our-array-dont-forget-to-wrap-the-array-in-an-array-as-it-was-a-scalar-value\">To insert a single array element into our array don&#8217;t forget to wrap the array in an array (as it was a scalar value!):<\/h4>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;%24element%20%3D%20array(&#8216;key1&#8217;%3D%3E&#8217;value1&#8217;)%3B%0A%24elements%20%3D%20array(%24element)%3B%0Aarray_splice(%24array%2C%20%24position%2C%20%24length%2C%20%24elements)%3B%0A&#8221; message=&#8221;Php Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM : How can we insert a new item into an array on any position, say for example in the middle of array? or How can we insert a new item into an array on any position? php arrays insert SOLUTION 1: It only requires one function call to array_splice: [pastacode lang=&#8221;php&#8221; manual=&#8221;%24original%20%3D%20array(%20%E2%80%98w&#8217;%2C%20%E2%80%98i&#8217;%2C%20%E2%80%98k&#8217;%2C%20%E2%80%98e&#8217;%2C%20%E2%80%98t&#8217;%20)%3B%0A%24inserted%20%3D%20array(%20&#8217;x&#8217;%20)%3B%20%2F%2F%20Not%20necessarily%20an%20array%0A%0Aarray_splice(%20%24original%2C%203%2C%200%2C%20%24inserted%20)%3B%20%2F%2F%20splice%20in%20at%20position%203%0A%2F%2F%20%24original%20is%20now%20w%20i%20k%20x%20e%20t%0A&#8221; message=&#8221;Php Code&#8221; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[2556,3906,3909,3908,3913,3911,3910,2562,3907,3912],"class_list":["post-1856","post","type-post","status-publish","format-standard","hentry","category-php","tag-how-to-check-if-php-array-is-associative-or-sequential","tag-how-to-insert-an-item-into-an-array-at-a-specific-index","tag-insert-array-content-into-database-php","tag-insert-array-of-new-item-in-any-position-in-another-array","tag-inserting-array-into-php-array","tag-php-insert-an-array-inside-a-multidimensionnal-array","tag-php-insert-item-at-any-array-position-with-key","tag-php-array-delete-by-value-not-key","tag-php-array-insert","tag-php-mysql-insert-array"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1856"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1856\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}